7 boost::asio::ip::tcp::resolver::query & query,
8 boost::asio::ip::tcp::resolver & resolver,
9 boost::asio::io_service & io_service
14 +
"Accept-Token: " +
token_ +
"\r\n"
15 +
"Connection: close\r\n"
16 +
"Content-Length: " + boost::lexical_cast<std::string>(
content_length)
19 std::ostream request_stream(&
request_);
24 timer_ = std::unique_ptr<boost::asio::deadline_timer>(
25 new boost::asio::deadline_timer(io_service));
31 socket_ = std::unique_ptr<boost::asio::ip::tcp::socket>(
32 new boost::asio::ip::tcp::socket(io_service));
34 resolver.async_resolve(query,
37 boost::asio::placeholders::error,
38 boost::asio::placeholders::iterator));
42 const boost::system::error_code & err,
43 boost::asio::ip::tcp::resolver::iterator endpoint_iterator
48 auto endpoint = * endpoint_iterator;
49 socket_->async_connect(endpoint,
52 boost::asio::placeholders::error,
53 ++endpoint_iterator));
60 const boost::system::error_code & err,
61 boost::asio::ip::tcp::resolver::iterator endpoint_iterator
66 timer_->expires_from_now(boost::posix_time::seconds(30));
67 boost::asio::async_write(*
socket_.get(),
71 boost::asio::placeholders::error));
73 else if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator()) {
75 auto endpoint = *endpoint_iterator;
76 socket_->async_connect(endpoint,
79 boost::asio::placeholders::error,
80 ++endpoint_iterator));
92 timer_->expires_from_now(boost::posix_time::seconds(30));
94 boost::asio::async_read_until(*
socket_.get(),
99 boost::asio::placeholders::error));
112 std::istream response_stream(&
response_);
113 std::string http_version;
114 response_stream >> http_version;
115 unsigned int status_code;
116 response_stream >> status_code;
117 std::string status_message;
118 std::getline( response_stream, status_message );
119 if (!response_stream || http_version.substr(0, 5) !=
"HTTP/") {
123 if (status_code != 200) {
127 timer_->expires_from_now(boost::posix_time::seconds(30));
130 boost::asio::async_read_until(*
socket_.get(),
135 boost::asio::placeholders::error));
147 timer_->expires_from_now(boost::posix_time::seconds(30));
149 boost::asio::async_read_until(*
socket_.get(),
154 boost::asio::placeholders::error,
155 boost::asio::placeholders::bytes_transferred));
167 timer_->expires_from_now(boost::posix_time::seconds(30));
169 boost::asio::async_read(*
socket_.get(),
171 boost::asio::transfer_at_least(1),
174 boost::asio::placeholders::error,
175 boost::asio::placeholders::bytes_transferred));
178 std::string buffer((std::istreambuf_iterator<char>(&
response_)),
179 std::istreambuf_iterator<char>());
185 std::string tmp = buffer;
189 throw std::runtime_error(
"no `Content-Length` in header response");
195 if (!buffer.empty()) {
205 else if (err != boost::asio::error::eof) {
209 else if (err == boost::asio::error::eof) {
211 std::cerr <<
"connection dropped unexpectedly" << std::endl;
227 timer_->expires_at(boost::posix_time::pos_infin);
233 if (
timer_->expires_at() <= boost::asio::deadline_timer::traits_type::now()) {
234 std::cerr <<
"connection time-out" << std::endl;
void handle_resolve(const boost::system::error_code &err, boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
Callback for Handling Address Resolution.
std::string header_
Header that will be used.
void invalid_request(const std::string message)
Handle Invalid Query - e.g.: response which states our query was invalid.
std::size_t content_length_
Content-Length.
void check_timeout()
check timed-out
void content_length(std::string response, std::size_t &length)
get the content leangth from
std::string post_
Actual post Data.
bool has_content_length(std::string response)
examine if the header response contains a content-length filed
void handle_connect(const boost::system::error_code &err, boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
void schedule(boost::asio::ip::tcp::resolver::query &query, boost::asio::ip::tcp::resolver &resolver, boost::asio::io_service &io_service)
std::atomic< bool > flag_
flag used to extract header
void reset()
reset handler (clear data, bytes, etc) and stop connection
boost::asio::streambuf response_
Response Container.
std::string strip_header(std::string response)
remove/strip the HTTP header and
boost::asio::streambuf request_
Request Container.
std::function< void(std::string)> callback_
Callback Handler - use with std::bind or boost variant.
void handle_read_headers(const boost::system::error_code &err)
Callback for Handling Headers.
void error_handler(const boost::system::error_code &error)
Handle an Error.
std::string json_
JSON reply.
const std::string token_
user authentication token
void handle_read_content(const boost::system::error_code &err, std::size_t bytes)
Callback for Handling Actual Data Contents.
void handle_read_status_line(const boost::system::error_code &err)
Callback for handling HTTP Header Response Data.
std::unique_ptr< boost::asio::ip::tcp::socket > socket_
TCP Socket (plain-text)
std::size_t bytes_transferred_
Bytes Transferred.
void handle_write_request(const boost::system::error_code &err)
Callback for handling request and waiting for response.
std::unique_ptr< boost::asio::deadline_timer > timer_
time-out timer
constexpr char address[]
api.rapp.cloud -