jeudi 17 septembre 2020

Generic boost::beast HTTP(s) client

How can I create generic boost::beast HTTP(s) client sends and receives any type of requests and responses? I have found only one solution to create template class

template <class Request, class Response>
class HttpsClient : public std::enable_shared_from_this<HttpsClient<Request, Response>>
{
public:
  using Handler = std::function<void(const std::error_code& ec, const Response& response)>;

  explicit HttpsClient(asio::io_context& ioc, ssl::context& ctx, std::string host, std::string service);

  void send(Request&& request, Handler&& handler);

private:
  void onResolve(const boost::system::error_code& ec, tcp::resolver::results_type results);
  void onConnect(const boost::system::error_code& ec, const tcp::endpoint& endpoint);
  void onHandshake(const boost::system::error_code& ec);

  boost::beast::flat_buffer         m_buffer;
  Request                           m_request;
  Response                          m_response;
};

I want to have an async public interface

void send(Request&& request, Handler&& handler);

which will do all needed work for resolving, connecting and handshaking and will be able to send any type of requests.

I can't make template function send because onResolve, onConnect and onHandshake have to be template functions too.

How can I resolve this task efficiently in terms of c++11?

Aucun commentaire:

Enregistrer un commentaire