Skip to content

Commit

Permalink
Added missing copyrights
Browse files Browse the repository at this point in the history
  • Loading branch information
yassiezar committed Jun 19, 2024
1 parent 8ea16b6 commit 14b3953
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
// Copyright 2021 RobosoftAI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*****************************************************************************************************************
*
Author: Jacobus Lock
******************************************************************************************************************/

#pragma once

#include <boost/asio/executor_work_guard.hpp>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
// Copyright 2021 RobosoftAI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*****************************************************************************************************************
*
Author: Jacobus Lock
******************************************************************************************************************/

#pragma once

#include <boost/asio/executor_work_guard.hpp>
Expand Down
78 changes: 50 additions & 28 deletions smacc2_client_library/http_client/src/http_client/http_session.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
// Copyright 2021 RobosoftAI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*****************************************************************************************************************
*
Author: Jacobus Lock
******************************************************************************************************************/

#include <http_client/http_session.hpp>

namespace cl_http
{
namespace cl_http {

http_session::http_session(
boost::asio::any_io_executor ioc, const std::function<void(const TResponse &)> response)
: onResponse{response}, resolver_{ioc}, stream_{ioc}
{
}
boost::asio::any_io_executor ioc,
const std::function<void(const TResponse &)> response)
: onResponse{response}, resolver_{ioc}, stream_{ioc} {}

void http_session::run(
const std::string & host, const std::string & target, const boost::beast::http::verb http_method,
const int & version)
{
void http_session::run(const std::string &host, const std::string &target,
const boost::beast::http::verb http_method,
const int &version) {
// Set up an HTTP request
req_.version(version);
req_.method(http_method);
Expand All @@ -21,26 +39,26 @@ void http_session::run(
req_.set(boost::beast::http::field::user_agent, BOOST_BEAST_VERSION_STRING);

// Look up the domain name
resolver_.async_resolve(
host.c_str(), kPort.c_str(),
boost::beast::bind_front_handler(&http_session::on_resolve, shared_from_this()));
resolver_.async_resolve(host.c_str(), kPort.c_str(),
boost::beast::bind_front_handler(
&http_session::on_resolve, shared_from_this()));
}

void http_session::on_resolve(
boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results)
{
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type results) {
if (ec) return fail(ec, "resolve");

// Set a timeout on the operation
stream_.expires_after(std::chrono::seconds(1));

// Make the connection on the IP address we get from a lookup
stream_.async_connect(
results, boost::beast::bind_front_handler(&http_session::on_connect, shared_from_this()));
results, boost::beast::bind_front_handler(&http_session::on_connect,
shared_from_this()));
}

void http_session::fail(boost::beast::error_code ec, char const * what)
{
void http_session::fail(boost::beast::error_code ec, char const *what) {
std::cout << "Failure!..." << std::endl;
std::cerr << what << ": " << ec.message() << "\n";
res_.result(boost::beast::http::status::bad_request);
Expand All @@ -49,32 +67,35 @@ void http_session::fail(boost::beast::error_code ec, char const * what)
}

void http_session::on_connect(
boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type::endpoint_type)
{
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type::endpoint_type) {
if (ec) return fail(ec, "connect");

// Set a timeout on the operation
stream_.expires_after(std::chrono::seconds(1));

// Send the HTTP request to the remote host
boost::beast::http::async_write(
stream_, req_, boost::beast::bind_front_handler(&http_session::on_write, shared_from_this()));
stream_, req_,
boost::beast::bind_front_handler(&http_session::on_write,
shared_from_this()));
}

void http_session::on_write(boost::beast::error_code ec, std::size_t bytes_transferred)
{
void http_session::on_write(boost::beast::error_code ec,
std::size_t bytes_transferred) {
boost::ignore_unused(bytes_transferred);

if (ec) return fail(ec, "write");

// Receive the HTTP response
boost::beast::http::async_read(
stream_, buffer_, res_,
boost::beast::bind_front_handler(&http_session::on_read, shared_from_this()));
stream_, buffer_, res_,
boost::beast::bind_front_handler(&http_session::on_read,
shared_from_this()));
}

void http_session::on_read(boost::beast::error_code ec, std::size_t bytes_transferred)
{
void http_session::on_read(boost::beast::error_code ec,
std::size_t bytes_transferred) {
boost::ignore_unused(bytes_transferred);

if (ec) return fail(ec, "read");
Expand All @@ -83,7 +104,8 @@ void http_session::on_read(boost::beast::error_code ec, std::size_t bytes_transf
stream_.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);

// not_connected happens sometimes so don't bother reporting it.
if (ec && ec != boost::beast::errc::not_connected) return fail(ec, "shutdown");
if (ec && ec != boost::beast::errc::not_connected)
return fail(ec, "shutdown");

// If we get here then the connection is closed gracefully
onResponse(res_);
Expand Down
106 changes: 64 additions & 42 deletions smacc2_client_library/http_client/src/http_client/ssl_http_session.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
// Copyright 2021 RobosoftAI Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*****************************************************************************************************************
*
Author: Jacobus Lock
******************************************************************************************************************/

#include <http_client/ssl_http_session.hpp>

namespace cl_http
{
namespace 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)
: onResponse{response}, resolver_{ioc}, stream_{ioc, ssl_context}
{
}
boost::asio::any_io_executor ioc, boost::asio::ssl::context &ssl_context,
const std::function<void(const TResponse &)> response)
: onResponse{response}, resolver_{ioc}, stream_{ioc, ssl_context} {}

void ssl_http_session::run(
const std::string & host, const std::string & target, const boost::beast::http::verb http_method,
const int & version)
{
void ssl_http_session::run(const std::string &host, const std::string &target,
const boost::beast::http::verb http_method,
const int &version) {
// Set up an HTTP request
req_.version(version);
req_.method(http_method);
Expand All @@ -23,25 +40,27 @@ void ssl_http_session::run(

// Look up the domain name
resolver_.async_resolve(
host.c_str(), kPort.c_str(),
boost::beast::bind_front_handler(&ssl_http_session::on_resolve, shared_from_this()));
host.c_str(), kPort.c_str(),
boost::beast::bind_front_handler(&ssl_http_session::on_resolve,
shared_from_this()));
}

void ssl_http_session::on_resolve(
boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results)
{
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type results) {
if (ec) return fail(ec, "resolve");

// Set a timeout on the operation
boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(1));
boost::beast::get_lowest_layer(stream_).expires_after(
std::chrono::seconds(1));

// Make the connection on the IP address we get from a lookup
boost::beast::get_lowest_layer(stream_).async_connect(
results, boost::beast::bind_front_handler(&ssl_http_session::on_connect, shared_from_this()));
results, boost::beast::bind_front_handler(&ssl_http_session::on_connect,
shared_from_this()));
}

void ssl_http_session::fail(boost::beast::error_code ec, char const * what)
{
void ssl_http_session::fail(boost::beast::error_code ec, char const *what) {
std::cout << "Failure!..." << std::endl;
std::cerr << what << ": " << ec.message() << "\n";
res_.result(boost::beast::http::status::bad_request);
Expand All @@ -50,63 +69,66 @@ void ssl_http_session::fail(boost::beast::error_code ec, char const * what)
}

void ssl_http_session::on_connect(
boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type::endpoint_type)
{
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type::endpoint_type) {
if (ec) return fail(ec, "connect");

// Set a timeout on the operation
boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(1));
boost::beast::get_lowest_layer(stream_).expires_after(
std::chrono::seconds(1));

// Send the HTTP request to the remote host
stream_.async_handshake(
boost::asio::ssl::stream_base::client,
boost::beast::bind_front_handler(&ssl_http_session::on_handshake, shared_from_this()));
boost::asio::ssl::stream_base::client,
boost::beast::bind_front_handler(&ssl_http_session::on_handshake,
shared_from_this()));
}

void ssl_http_session::on_handshake(boost::beast::error_code ec)
{
void ssl_http_session::on_handshake(boost::beast::error_code ec) {
if (ec) return fail(ec, "handshake");

// Set a timeout on the operation
boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(1));
boost::beast::get_lowest_layer(stream_).expires_after(
std::chrono::seconds(1));

// Send the HTTP request to the remote host
boost::beast::http::async_write(
stream_, req_,
boost::beast::bind_front_handler(&ssl_http_session::on_write, shared_from_this()));
stream_, req_,
boost::beast::bind_front_handler(&ssl_http_session::on_write,
shared_from_this()));
}

void ssl_http_session::on_write(boost::beast::error_code ec, std::size_t bytes_transferred)
{
void ssl_http_session::on_write(boost::beast::error_code ec,
std::size_t bytes_transferred) {
boost::ignore_unused(bytes_transferred);

if (ec) return fail(ec, "write");

// Receive the HTTP response
boost::beast::http::async_read(
stream_, buffer_, res_,
boost::beast::bind_front_handler(&ssl_http_session::on_read, shared_from_this()));
stream_, buffer_, res_,
boost::beast::bind_front_handler(&ssl_http_session::on_read,
shared_from_this()));
}

void ssl_http_session::on_read(boost::beast::error_code ec, std::size_t bytes_transferred)
{
void ssl_http_session::on_read(boost::beast::error_code ec,
std::size_t bytes_transferred) {
boost::ignore_unused(bytes_transferred);

if (ec) return fail(ec, "read");

boost::beast::get_lowest_layer(stream_).expires_after(std::chrono::seconds(1));
boost::beast::get_lowest_layer(stream_).expires_after(
std::chrono::seconds(1));

// Gracefully close the socket
// stream_.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
stream_.async_shutdown(
boost::beast::bind_front_handler(&ssl_http_session::on_shutdown, shared_from_this()));
stream_.async_shutdown(boost::beast::bind_front_handler(
&ssl_http_session::on_shutdown, shared_from_this()));
}

void ssl_http_session::on_shutdown(boost::beast::error_code ec)
{
void ssl_http_session::on_shutdown(boost::beast::error_code ec) {
// not_connected happens sometimes so don't bother reporting it.
if (ec == boost::asio::error::eof)
{
if (ec == boost::asio::error::eof) {
// Rationale:
// http://stackoverflow.com/questions/25587403/boost-asio-ssl-async-shutdown-always-finishes-with-an-error
ec = {};
Expand Down

0 comments on commit 14b3953

Please sign in to comment.