25#ifndef JOIN_SERVICES_HTTP_CLIENT_HPP
26#define JOIN_SERVICES_HTTP_CLIENT_HPP
36#include <join/version.hpp>
46 template <
class Protocol>
47 class BasicHttpClient :
public Protocol::Stream
95 : Protocol::Stream (
std::move (other))
111 Protocol::Stream::operator= (std::move (other));
112 this->
_host = std::move (other._host);
113 this->
_port = other._port;
114 this->
_keep = other._keep;
134 Protocol::Stream::close ();
152 const std::string&
host ()
const
176 auth +=
"[" + this->
host () +
"]";
180 auth += this->
host ();
185 auth +=
":" + std::to_string (this->
port ());
250 endpoint.hostname (this->
host ());
262 request.
header (
"Accept",
"*/*");
266 request.
header (
"Connection", this->
_keep ?
"keep-alive" :
"close");
274 request.
header (
"User-Agent",
"join/" JOIN_VERSION);
287 if (request.
hasHeader (
"Transfer-Encoding"))
291 if (request.
hasHeader (
"Content-Encoding"))
316 std::string connection = response.
header (
"Connection");
317 std::string alive = response.
header (
"Keep-Alive");
322 size_t pos = alive.find (
"timeout=");
323 if (pos != std::string::npos)
326 std::chrono::seconds (::atoi (alive.substr (pos + 8, alive.find (
",", pos + 8)).c_str ()));
329 pos = alive.find (
"max=");
330 if (pos != std::string::npos)
332 this->
_keepMax = ::atoi (alive.substr (pos + 4, alive.find (
",", pos + 4)).c_str ());
342 if (response.
hasHeader (
"Transfer-Encoding"))
346 if (response.
hasHeader (
"Content-Encoding"))
352 this->
_timestamp = std::chrono::steady_clock::now ();
364 for (
auto const& encoding : encodings)
366 if (encoding.find (
"gzip") != std::string::npos)
371 else if (encoding.find (
"deflate") != std::string::npos)
376 else if (encoding.find (
"chunked") != std::string::npos)
409 auto duration = std::chrono::steady_clock::now () - this->
_timestamp;
410 return (this->
_keepTimeout < std::chrono::duration_cast<std::chrono::seconds> (duration)) ||
420 return !this->connected () || this->
expired ();
431 this->connect (endpoint);
441 std::chrono::time_point<std::chrono::steady_clock>
_timestamp;
465 template <
class Protocol>
478 template <
class Protocol>
481 in.receive (response);
488 template <
class Protocol>
489 class BasicHttpSecureClient :
public BasicHttpClient<Protocol>
493 using Stream =
typename Protocol::Stream;
506 static_cast<Stream&
> (*this) =
Stream (std::move (ctx));
578 this->connect (endpoint);
592 template <
class Protocol>
605 template <
class Protocol>
608 in.receive (response);
static IpAddress lookupAddress(const std::string &host, int family)
resolve host name using system name servers.
Definition resolver.hpp:260
static uint16_t resolveService(const std::string &service) noexcept
resolve service name.
Definition resolver.hpp:673
basic HTTP client.
Definition http_protocol.hpp:34
bool _keep
HTTP keep alive.
Definition http_client.hpp:450
bool _wrapped
HTTP stream status.
Definition http_client.hpp:438
std::string url() const
get URL.
Definition http_client.hpp:195
std::chrono::time_point< std::chrono::steady_clock > _timestamp
HTTP timestamp.
Definition http_client.hpp:441
std::string _host
HTTP host.
Definition http_client.hpp:444
BasicHttpClient(const char *host, uint16_t port=Protocol::defaultPort, bool keepAlive=true)
create the basic HTTP client instance.
Definition http_client.hpp:58
BasicHttpClient(const std::string &host, uint16_t port=Protocol::defaultPort, bool keepAlive=true)
create the basic HTTP client instance.
Definition http_client.hpp:72
uint16_t _port
HTTP port.
Definition http_client.hpp:447
uint16_t port() const
get port.
Definition http_client.hpp:161
void close() override
close the connection.
Definition http_client.hpp:132
int _keepMax
HTTP keep alive max.
Definition http_client.hpp:456
void setEncoding(const std::vector< std::string > &encodings)
set stream encoding.
Definition http_client.hpp:362
const std::string & host() const
get host.
Definition http_client.hpp:152
BasicHttpClient & operator=(const BasicHttpClient &other)=delete
assign the basic HTTP client instance by copy.
BasicHttpClient(const BasicHttpClient &other)=delete
create the basic HTTP client instance by copy.
virtual ~BasicHttpClient()
destroy the basic HTTP client instance.
Definition http_client.hpp:123
virtual void reconnect(const Endpoint &endpoint)
perform reconnection to the given endpoint.
Definition http_client.hpp:427
int keepAliveMax() const
get HTTP keep alive max.
Definition http_client.hpp:231
int send(HttpRequest &request)
send HTTP request.
Definition http_client.hpp:241
typename Protocol::Endpoint Endpoint
Definition http_client.hpp:50
std::chrono::seconds _keepTimeout
HTTP keep alive timeout.
Definition http_client.hpp:453
void keepAlive(bool keep)
enable/disable HTTP keep alive support.
Definition http_client.hpp:213
void clearEncoding()
clear stream encoding.
Definition http_client.hpp:389
virtual std::string scheme() const
get HTTP scheme.
Definition http_client.hpp:143
bool needReconnection()
check if client must reconnect.
Definition http_client.hpp:418
int receive(HttpResponse &response)
receive HTTP response.
Definition http_client.hpp:304
std::string authority() const
get authority.
Definition http_client.hpp:170
std::chrono::seconds keepAliveTimeout() const
get HTTP keep alive timeout.
Definition http_client.hpp:222
bool expired() const
check if HTTP keep alive is expired.
Definition http_client.hpp:407
std::streambuf * _streambuf
HTTP stream buffer.
Definition http_client.hpp:435
bool keepAlive() const
checks if HTTP keep alive is supported.
Definition http_client.hpp:204
BasicHttpClient(BasicHttpClient &&other)
create the basic HTTP client instance by move.
Definition http_client.hpp:94
basic HTTPS client.
Definition http_protocol.hpp:37
std::string scheme() const override
get HTTP scheme.
Definition http_client.hpp:564
typename Protocol::Stream Stream
Definition http_client.hpp:493
void reconnect(const Endpoint &endpoint) override
perform reconnection to the given endpoint.
Definition http_client.hpp:574
BasicHttpSecureClient(BasicHttpSecureClient &&other)
create the basic HTTPS client instance by move.
Definition http_client.hpp:539
virtual ~BasicHttpSecureClient()=default
destroy the basic HTTPS client instance.
BasicHttpSecureClient(TlsContext ctx, const std::string &host, uint16_t port=Protocol::defaultPort, bool keepAlive=true)
create the basic HTTPS client instance using the given context.
Definition http_client.hpp:516
BasicHttpSecureClient(TlsContext ctx, const char *host, uint16_t port=Protocol::defaultPort, bool keepAlive=true)
create the basic HTTPS client instance using the given context.
Definition http_client.hpp:502
BasicHttpSecureClient(const BasicHttpSecureClient &other)=delete
create the basic HTTPS client instance by copy.
typename Protocol::Endpoint Endpoint
Definition http_client.hpp:492
BasicHttpSecureClient & operator=(const BasicHttpSecureClient &other)=delete
assign the basic HTTPS client instance by copy.
chunk stream buffer.
Definition chunk_stream.hpp:41
virtual int readHeaders(std::istream &in)
read HTTP header from the given input stream.
Definition http_message.cpp:304
bool hasHeader(const std::string &name) const
checks if there is a header with the specified name.
Definition http_message.cpp:203
std::string header(const std::string &name) const
get header by name.
Definition http_message.cpp:212
HTTP request.
Definition http_message.hpp:352
virtual int writeHeaders(std::ostream &out) const override
write HTTP headers to the given output stream.
Definition http_message.cpp:666
HTTP response.
Definition http_message.hpp:558
bool isIpv6Address() const
check if IP address is an IPv6 address.
Definition ip_address.cpp:1394
TLS/DTLS context.
Definition tls_context.hpp:42
@ Deflate
Definition zstream.hpp:132
@ Gzip
Definition zstream.hpp:134
zlib stream buffer.
Definition zstream.hpp:44
Definition acceptor.hpp:32
constexpr BasicHttpClient< Protocol > & operator>>(BasicHttpClient< Protocol > &in, HttpResponse &response)
read HTTP response from the HTTP stream.
Definition http_client.hpp:479
std::ostream & operator<<(std::ostream &os, const BasicLinkLayerEndpoint< Protocol > &endpoint)
push endpoint representation into a stream.
Definition endpoint.hpp:258
bool compareNoCase(const std::string &a, const std::string &b)
case insensitive string comparison.
Definition utils.hpp:195
std::vector< std::string > rsplit(const std::string &in, const std::string &delim)
split a string in reverse order using a delimiter.
Definition utils.hpp:282