From 14b39539d02441cfa0747a1340d7bfbb87be9d0c Mon Sep 17 00:00:00 2001 From: Jaycee Lock Date: Wed, 19 Jun 2024 18:24:30 +0100 Subject: [PATCH] Added missing copyrights --- .../include/http_client/http_session.hpp | 21 ++++ .../include/http_client/ssl_http_session.hpp | 21 ++++ .../src/http_client/http_session.cpp | 78 ++++++++----- .../src/http_client/ssl_http_session.cpp | 106 +++++++++++------- 4 files changed, 156 insertions(+), 70 deletions(-) diff --git a/smacc2_client_library/http_client/include/http_client/http_session.hpp b/smacc2_client_library/http_client/include/http_client/http_session.hpp index 2e482cd3..3235fd9d 100644 --- a/smacc2_client_library/http_client/include/http_client/http_session.hpp +++ b/smacc2_client_library/http_client/include/http_client/http_session.hpp @@ -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 diff --git a/smacc2_client_library/http_client/include/http_client/ssl_http_session.hpp b/smacc2_client_library/http_client/include/http_client/ssl_http_session.hpp index b9fd8814..551edfc9 100644 --- a/smacc2_client_library/http_client/include/http_client/ssl_http_session.hpp +++ b/smacc2_client_library/http_client/include/http_client/ssl_http_session.hpp @@ -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 diff --git a/smacc2_client_library/http_client/src/http_client/http_session.cpp b/smacc2_client_library/http_client/src/http_client/http_session.cpp index fc84e6e3..2f5012a0 100644 --- a/smacc2_client_library/http_client/src/http_client/http_session.cpp +++ b/smacc2_client_library/http_client/src/http_client/http_session.cpp @@ -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 -namespace cl_http -{ +namespace cl_http { http_session::http_session( - boost::asio::any_io_executor ioc, const std::function response) -: onResponse{response}, resolver_{ioc}, stream_{ioc} -{ -} + boost::asio::any_io_executor ioc, + const std::function 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); @@ -21,14 +39,14 @@ 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 @@ -36,11 +54,11 @@ void http_session::on_resolve( // 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); @@ -49,8 +67,8 @@ 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 @@ -58,23 +76,26 @@ void http_session::on_connect( // 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"); @@ -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_); diff --git a/smacc2_client_library/http_client/src/http_client/ssl_http_session.cpp b/smacc2_client_library/http_client/src/http_client/ssl_http_session.cpp index c3373720..8d0ff5ef 100644 --- a/smacc2_client_library/http_client/src/http_client/ssl_http_session.cpp +++ b/smacc2_client_library/http_client/src/http_client/ssl_http_session.cpp @@ -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 -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 response) -: onResponse{response}, resolver_{ioc}, stream_{ioc, ssl_context} -{ -} + boost::asio::any_io_executor ioc, boost::asio::ssl::context &ssl_context, + const std::function 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); @@ -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); @@ -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 = {};