blob: e5c40df6b03886c07cd3e02879588dda2038ad2d [file] [log] [blame] [edit]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HTTP_HTTP_CLIENT_INTERFACE_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HTTP_HTTP_CLIENT_INTERFACE_H_
#include <cstddef>
#include <string>
#include <string_view>
#include "absl/functional/any_invocable.h"
#include "absl/status/status.h"
#include "boost/asio.hpp" //NOLINT: boost::asio is commonly used in BMC
#include "boost/asio.hpp" // NOLINT: boost::asio is commonly used in BMC
#include "boost/asio/error.hpp" // NOLINT
#include "boost/asio/random_access_file.hpp" // NOLINT: boost::asio is commonly used in BMC
#include "boost/beast/core.hpp" // NOLINT: boost::beast is commonly used in BMC
#include "boost/beast/http.hpp" // NOLINT: boost::beast is commonly used in BMC
#include "boost/beast/version.hpp" // NOLINT: boost::beast is commonly used in BMC
#include "boost/filesystem.hpp" // NOLINT: boost::filesystem is commonly used in BMC
#include "boost/filesystem/operations.hpp" // NOLINT
#include "boost/system/detail/error_code.hpp" // NOLINT: boost::asio is commonly used in BMC
namespace milotic_tlbmc {
class HttpClientInterface {
public:
HttpClientInterface() = default;
virtual ~HttpClientInterface() = default;
// ScanJob is a struct that holds the target URI and callback for the job. We
// could move the content of this struct to HttpClient, but for future use
// cases, we can have multiple jobs running on the same http client at the
// same time but with different target URIs and callback, we keep it this way.
struct ScanJob {
// Networking objects
boost::beast::http::request<boost::beast::http::string_body> req;
boost::beast::http::response<boost::beast::http::string_body> res;
// Target URI to send the request to.
std::string target;
// Callback for the job's final completion
absl::AnyInvocable<void(
boost::beast::error_code,
boost::beast::http::response<boost::beast::http::string_body>)>
callback;
};
// Returns true if there is an active job running, indicating http client is
// busy.
virtual bool IsActive() = 0;
// Sends a request to the given host, port, target, io_context and callback.
// Returns true if the request is sent successfully, false otherwise.
virtual absl::Status SendRequest(
boost::asio::io_context& io_context, std::string_view host,
std::string_view port, std::string_view target,
absl::AnyInvocable<
void(boost::beast::error_code,
boost::beast::http::response<boost::beast::http::string_body>)>
callback) = 0;
// Resolve the host and port from ScanJob.
virtual void OnResolve(
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type results) = 0;
// Connect to the resolved IP address.
virtual void OnConnect(
boost::beast::error_code ec,
boost::asio::ip::tcp::resolver::results_type::endpoint_type endpoint) = 0;
// Write the request to the stream.
virtual void OnWrite(boost::beast::error_code ec,
std::size_t bytes_transferred) = 0;
// Read the response from the stream.
virtual void OnRead(boost::beast::error_code ec,
std::size_t bytes_transferred) = 0;
};
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_HTTP_HTTP_CLIENT_INTERFACE_H_