-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
457 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
smacc2_client_library/http_client/include/http_client/http_session.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#pragma once | ||
|
||
#include <boost/asio/executor_work_guard.hpp> | ||
#include <boost/asio/strand.hpp> | ||
#include <boost/beast/core.hpp> | ||
#include <boost/beast/http.hpp> | ||
#include <boost/beast/version.hpp> | ||
#include <http_client/http_session_base.hpp> | ||
#include <iostream> | ||
#include <string> | ||
|
||
namespace cl_http | ||
{ | ||
class http_session : public std::enable_shared_from_this<http_session>, public http_session_base | ||
{ | ||
public: | ||
// Objects are constructed with a strand to | ||
// ensure that handlers do not execute concurrently. | ||
http_session( | ||
boost::asio::any_io_executor ioc, const std::function<void(const TResponse &)> response); | ||
|
||
virtual ~http_session() {} | ||
|
||
// Start the asynchronous operation | ||
void run( | ||
const std::string & host, const std::string & target, | ||
const boost::beast::http::verb http_method, const int & version) override; | ||
|
||
std::string getPort() override { return kPort; } | ||
|
||
private: | ||
const std::string kPort = "80"; | ||
|
||
void on_resolve( | ||
boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) override; | ||
void fail(boost::beast::error_code ec, const char * what) override; | ||
void on_connect( | ||
boost::beast::error_code ec, | ||
boost::asio::ip::tcp::resolver::results_type::endpoint_type) override; | ||
void on_write(boost::beast::error_code ec, std::size_t bytes_transferred) override; | ||
void on_read(boost::beast::error_code ec, std::size_t bytes_transferred) override; | ||
|
||
std::function<void(const TResponse &)> onResponse; | ||
|
||
boost::asio::ip::tcp::resolver resolver_; | ||
boost::beast::tcp_stream stream_; | ||
boost::beast::flat_buffer buffer_; // (Must persist between reads) | ||
boost::beast::http::request<boost::beast::http::empty_body> req_; | ||
boost::beast::http::response<boost::beast::http::string_body> res_; | ||
}; | ||
} // namespace cl_http |
40 changes: 40 additions & 0 deletions
40
smacc2_client_library/http_client/include/http_client/http_session_base.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include <boost/asio/executor_work_guard.hpp> | ||
#include <boost/asio/strand.hpp> | ||
#include <boost/beast/core.hpp> | ||
#include <boost/beast/http.hpp> | ||
#include <boost/beast/ssl.hpp> | ||
#include <boost/beast/version.hpp> | ||
#include <string> | ||
|
||
namespace cl_http | ||
{ | ||
class http_session_base | ||
{ | ||
public: | ||
virtual ~http_session_base() {} | ||
|
||
using TResponse = boost::beast::http::response<boost::beast::http::string_body>; | ||
|
||
// Start the asynchronous operation | ||
virtual void run( | ||
const std::string & host, const std::string & target, | ||
const boost::beast::http::verb http_method, const int & version) = 0; | ||
|
||
virtual std::string getPort() = 0; | ||
|
||
protected: | ||
virtual void on_resolve( | ||
boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) = 0; | ||
virtual void fail(boost::beast::error_code ec, const char * what) = 0; | ||
virtual void on_connect( | ||
boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type::endpoint_type) = 0; | ||
virtual void on_write(boost::beast::error_code ec, std::size_t bytes_transferred) = 0; | ||
virtual void on_read(boost::beast::error_code ec, std::size_t bytes_transferred) = 0; | ||
|
||
// Optional, needed for SSL connections | ||
virtual void on_handshake(boost::beast::error_code ec) {} | ||
virtual void on_shutdown(boost::beast::error_code ec) {} | ||
}; | ||
} // namespace cl_http |
57 changes: 57 additions & 0 deletions
57
smacc2_client_library/http_client/include/http_client/ssl_http_session.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#pragma once | ||
|
||
#include <boost/asio/executor_work_guard.hpp> | ||
#include <boost/asio/strand.hpp> | ||
#include <boost/beast/core.hpp> | ||
#include <boost/beast/http.hpp> | ||
#include <boost/beast/ssl.hpp> | ||
#include <boost/beast/version.hpp> | ||
#include <http_client/http_session_base.hpp> | ||
#include <iostream> | ||
#include <string> | ||
|
||
namespace cl_http | ||
{ | ||
class ssl_http_session : public std::enable_shared_from_this<ssl_http_session>, | ||
public http_session_base | ||
{ | ||
public: | ||
// Objects are constructed with a strand to | ||
// ensure that handlers do not execute concurrently. | ||
ssl_http_session( | ||
boost::asio::any_io_executor ioc, boost::asio::ssl::context & ssl_context, | ||
const std::function<void(const TResponse &)> response); | ||
|
||
virtual ~ssl_http_session() {} | ||
|
||
// Start the asynchronous operation | ||
void run( | ||
const std::string & host, const std::string & target, | ||
const boost::beast::http::verb http_method, const int & version) override; | ||
|
||
std::string getPort() override { return kPort; } | ||
|
||
private: | ||
const std::string kPort = "443"; | ||
|
||
void on_resolve( | ||
boost::beast::error_code ec, boost::asio::ip::tcp::resolver::results_type results) override; | ||
void fail(boost::beast::error_code ec, const char * what) override; | ||
void on_connect( | ||
boost::beast::error_code ec, | ||
boost::asio::ip::tcp::resolver::results_type::endpoint_type) override; | ||
void on_handshake(boost::beast::error_code ec) override; | ||
void on_write(boost::beast::error_code ec, std::size_t bytes_transferred) override; | ||
void on_read(boost::beast::error_code ec, std::size_t bytes_transferred) override; | ||
void on_shutdown(boost::beast::error_code ec) override; | ||
|
||
std::function<void(const TResponse &)> onResponse; | ||
|
||
boost::asio::ip::tcp::resolver resolver_; | ||
// boost::beast::tcp_stream stream_; | ||
boost::beast::ssl_stream<boost::beast::tcp_stream> stream_; | ||
boost::beast::flat_buffer buffer_; // (Must persist between reads) | ||
boost::beast::http::request<boost::beast::http::empty_body> req_; | ||
boost::beast::http::response<boost::beast::http::string_body> res_; | ||
}; | ||
} // namespace cl_http |
Oops, something went wrong.