17#include <drogon/exports.h>
19#include <drogon/drogon_callbacks.h>
22#include <trantor/utils/NonCopyable.h>
23#include <trantor/net/EventLoop.h>
30#ifdef __cpp_impl_coroutine
37using HttpClientPtr = std::shared_ptr<HttpClient>;
38#ifdef __cpp_impl_coroutine
41struct HttpRespAwaiter :
public CallbackAwaiter<HttpResponsePtr>
43 HttpRespAwaiter(HttpClient *client, HttpRequestPtr req,
double timeout)
44 : client_(client), req_(std::move(req)), timeout_(timeout)
48 void await_suspend(std::coroutine_handle<> handle);
72class DROGON_EXPORT HttpClient :
public trantor::NonCopyable
92 const HttpReqCallback &callback,
93 double timeout = 0) = 0;
112 HttpReqCallback &&callback,
113 double timeout = 0) = 0;
130 std::pair<ReqResult, HttpResponsePtr>
sendRequest(
const HttpRequestPtr &req,
133 assert(!
getLoop()->isInLoopThread() &&
134 "Deadlock detected! Calling a sync API from the same loop as "
135 "the HTTP client processes on will deadlock the event loop");
136 std::promise<std::pair<ReqResult, HttpResponsePtr>> prom;
137 auto f = prom.get_future();
140 [&prom](ReqResult r,
const HttpResponsePtr &resp) {
141 prom.set_value({r, resp});
147#ifdef __cpp_impl_coroutine
160 internal::HttpRespAwaiter sendRequestCoro(HttpRequestPtr req,
163 return internal::HttpRespAwaiter(
this, std::move(req), timeout);
213 const std::string &value) = 0;
254 trantor::EventLoop *loop =
nullptr,
255 bool useOldTLS =
false,
256 bool validateCert =
true);
263 virtual size_t bytesReceived()
const = 0;
265 virtual std::string host()
const = 0;
267 std::string getHost()
const
272 virtual uint16_t port()
const = 0;
274 uint16_t getPort()
const
279 virtual bool secure()
const = 0;
281 bool onDefaultPort()
const
284 return port() == 443;
297 const std::string &key) = 0;
310 const std::vector<std::pair<std::string, std::string>>
345 trantor::EventLoop *loop =
nullptr,
346 bool useOldTLS =
false,
347 bool validateCert =
true);
349 virtual ~HttpClient()
357#ifdef __cpp_impl_coroutine
359class HttpException :
public std::exception
362 HttpException() =
delete;
364 explicit HttpException(ReqResult res)
365 : resultCode_(res), message_(to_string_view(res))
369 const char *what() const noexcept
override
371 return message_.data();
374 ReqResult code()
const
380 ReqResult resultCode_;
381 std::string_view message_;
384inline void internal::HttpRespAwaiter::await_suspend(
385 std::coroutine_handle<> handle)
387 assert(client_ !=
nullptr);
388 assert(req_ !=
nullptr);
389 client_->sendRequest(
391 [handle,
this](ReqResult result,
const HttpResponsePtr &resp) {
392 if (result == ReqResult::Ok)
395 setException(std::make_exception_ptr(HttpException(result)));
this class represents a cookie entity.
Definition Cookie.h:32
Asynchronous http client.
Definition HttpClient.h:73
static HttpClientPtr newHttpClient(const std::string &hostString, trantor::EventLoop *loop=nullptr, bool useOldTLS=false, bool validateCert=true)
Create a Http client using the hostString to connect to server.
virtual void setSockOptCallback(std::function< void(int)> cb)=0
Set socket options(before connecting).
virtual void sendRequest(const HttpRequestPtr &req, HttpReqCallback &&callback, double timeout=0)=0
Send a request asynchronously to the server.
virtual trantor::EventLoop * getLoop()=0
Get the event loop of the client;.
virtual void addCookie(const std::string &key, const std::string &value)=0
Add a cookie to the client.
virtual void setUserAgent(const std::string &userAgent)=0
Set the user_agent header, the default value is 'DrogonClient' if this method is not used.
static HttpClientPtr newHttpClient(const std::string &ip, uint16_t port, bool useSSL=false, trantor::EventLoop *loop=nullptr, bool useOldTLS=false, bool validateCert=true)
Create a new HTTP client which use ip and port to connect to server.
virtual void sendRequest(const HttpRequestPtr &req, const HttpReqCallback &callback, double timeout=0)=0
Send a request asynchronously to the server.
virtual size_t bytesSent() const =0
Get the number of bytes sent or received.
std::pair< ReqResult, HttpResponsePtr > sendRequest(const HttpRequestPtr &req, double timeout=0)
Send a request synchronously to the server and return the response.
Definition HttpClient.h:130
virtual void setCertPath(const std::string &cert, const std::string &key)=0
Set the client certificate used by the HTTP connection.
virtual void addCookie(const Cookie &cookie)=0
Add a cookie to the client.
virtual void enableCookies(bool flag=true)=0
Enable cookies for the client.
virtual void setPipeliningDepth(size_t depth)=0
virtual void addSSLConfigs(const std::vector< std::pair< std::string, std::string > > &sslConfCmds)=0
Supplies command style options for SSL_CONF_cmd.
virtual std::size_t requestsBufferSize()=0
Return the number of unsent http requests in the current http client cache buffer.
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23