25#ifndef JOIN_SERVICES_HTTPCLIENT_HPP
26#define JOIN_SERVICES_HTTPCLIENT_HPP
29#include <join/version.hpp>
44 template <
class Protocol>
93 : Protocol::Stream (
std::move (other))
109 Protocol::Stream::operator= (std::move (other));
110 this->
_host = std::move (other._host);
111 this->
_port = other._port;
112 this->
_keep = other._keep;
132 Protocol::Stream::close ();
150 const std::string&
host ()
const
174 auth +=
"[" + this->
host () +
"]";
178 auth += this->
host ();
183 auth +=
":" + std::to_string (this->
port ());
248 endpoint.hostname (this->
host ());
260 request.
header (
"Accept",
"*/*");
264 request.
header (
"Connection", this->
_keep ?
"keep-alive" :
"close");
272 request.
header (
"User-Agent",
"join/" JOIN_VERSION);
285 if (request.
hasHeader (
"Transfer-Encoding"))
289 if (request.
hasHeader (
"Content-Encoding"))
314 std::string connection = response.
header (
"Connection");
315 std::string alive = response.
header (
"Keep-Alive");
320 size_t pos = alive.find (
"timeout=");
321 if (pos != std::string::npos)
324 std::chrono::seconds (::atoi (alive.substr (pos + 8, alive.find (
",", pos + 8)).c_str ()));
327 pos = alive.find (
"max=");
328 if (pos != std::string::npos)
330 this->
_keepMax = ::atoi (alive.substr (pos + 4, alive.find (
",", pos + 4)).c_str ());
340 if (response.
hasHeader (
"Transfer-Encoding"))
344 if (response.
hasHeader (
"Content-Encoding"))
350 this->
_timestamp = std::chrono::steady_clock::now ();
362 for (
auto const& encoding : encodings)
364 if (encoding.find (
"gzip") != std::string::npos)
369 else if (encoding.find (
"deflate") != std::string::npos)
374 else if (encoding.find (
"chunked") != std::string::npos)
407 auto duration = std::chrono::steady_clock::now () - this->
_timestamp;
408 return (this->
_keepTimeout < std::chrono::duration_cast<std::chrono::seconds> (duration)) ||
418 return !this->connected () || this->
expired ();
429 this->connect (endpoint);
439 std::chrono::time_point<std::chrono::steady_clock>
_timestamp;
463 template <
class Protocol>
476 template <
class Protocol>
486 template <
class Protocol>
570 this->connectEncrypted (endpoint);
580 template <
class Protocol>
593 template <
class Protocol>
basic HTTP client.
Definition httpclient.hpp:46
bool _keep
HTTP keep alive.
Definition httpclient.hpp:448
bool _wrapped
HTTP stream status.
Definition httpclient.hpp:436
std::string url() const
get URL.
Definition httpclient.hpp:193
std::chrono::time_point< std::chrono::steady_clock > _timestamp
HTTP timestamp.
Definition httpclient.hpp:439
std::string _host
HTTP host.
Definition httpclient.hpp:442
uint16_t _port
HTTP port.
Definition httpclient.hpp:445
uint16_t port() const
get port.
Definition httpclient.hpp:159
void close() override
close the connection.
Definition httpclient.hpp:130
int _keepMax
HTTP keep alive max.
Definition httpclient.hpp:454
void setEncoding(const std::vector< std::string > &encodings)
set stream encoding.
Definition httpclient.hpp:360
const std::string & host() const
get host.
Definition httpclient.hpp:150
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 httpclient.hpp:121
virtual void reconnect(const Endpoint &endpoint)
perform reconnection to the given endpoint.
Definition httpclient.hpp:425
int keepAliveMax() const
get HTTP keep alive max.
Definition httpclient.hpp:229
int send(HttpRequest &request)
send HTTP request.
Definition httpclient.hpp:239
typename Protocol::Endpoint Endpoint
Definition httpclient.hpp:48
std::chrono::seconds _keepTimeout
HTTP keep alive timeout.
Definition httpclient.hpp:451
BasicHttpClient(const char *host, uint16_t port=80, bool keepAlive=true)
create the basic HTTP client instance.
Definition httpclient.hpp:56
void keepAlive(bool keep)
enable/disable HTTP keep alive support.
Definition httpclient.hpp:211
void clearEncoding()
clear stream encoding.
Definition httpclient.hpp:387
virtual std::string scheme() const
get HTTP scheme.
Definition httpclient.hpp:141
bool needReconnection()
check if client must reconnect.
Definition httpclient.hpp:416
int receive(HttpResponse &response)
receive HTTP response.
Definition httpclient.hpp:302
std::string authority() const
get authority.
Definition httpclient.hpp:168
std::chrono::seconds keepAliveTimeout() const
get HTTP keep alive timeout.
Definition httpclient.hpp:220
bool expired() const
check if HTTP keep alive is expired.
Definition httpclient.hpp:405
std::streambuf * _streambuf
HTTP stream buffer.
Definition httpclient.hpp:433
bool keepAlive() const
checks if HTTP keep alive is supported.
Definition httpclient.hpp:202
BasicHttpClient(const std::string &host, uint16_t port=80, bool keepAlive=true)
create the basic HTTP client instance.
Definition httpclient.hpp:70
BasicHttpClient(BasicHttpClient &&other)
create the basic HTTP client instance by move.
Definition httpclient.hpp:92
basic HTTPS client.
Definition httpclient.hpp:488
std::string scheme() const override
get HTTP scheme.
Definition httpclient.hpp:556
void reconnect(const Endpoint &endpoint) override
perform reconnection to the given endpoint.
Definition httpclient.hpp:566
BasicHttpSecureClient(const char *host, uint16_t port=443, bool keepAlive=true)
create the basic HTTPS client instance.
Definition httpclient.hpp:498
BasicHttpSecureClient(BasicHttpSecureClient &&other)
create the basic HTTPS client instance by move.
Definition httpclient.hpp:531
BasicHttpSecureClient(const std::string &host, uint16_t port=443, bool keepAlive=true)
create the basic HTTPS client instance.
Definition httpclient.hpp:509
virtual ~BasicHttpSecureClient()=default
destroy the basic HTTPS client instance.
BasicHttpSecureClient(const BasicHttpSecureClient &other)=delete
create the basic HTTPS client instance by copy.
typename Protocol::Endpoint Endpoint
Definition httpclient.hpp:490
BasicHttpSecureClient & operator=(const BasicHttpSecureClient &other)=delete
assign the basic HTTPS client instance by copy.
chunk stream buffer.
Definition chunkstream.hpp:37
virtual int readHeaders(std::istream &in)
read HTTP header from the given input stream.
Definition httpmessage.cpp:304
bool hasHeader(const std::string &name) const
checks if there is a header with the specified name.
Definition httpmessage.cpp:203
std::string header(const std::string &name) const
get header by name.
Definition httpmessage.cpp:212
HTTP request.
Definition httpmessage.hpp:353
virtual int writeHeaders(std::ostream &out) const override
write HTTP headers to the given output stream.
Definition httpmessage.cpp:666
HTTP response.
Definition httpmessage.hpp:559
bool isIpv6Address() const
check if IP address is an IPv6 address.
Definition ipaddress.cpp:1394
static uint16_t resolveService(const std::string &service)
resolve service name.
Definition resolver.cpp:550
IpAddress resolveHost(const std::string &host, int family, const IpAddress &server, uint16_t port=dnsPort, int timeout=5000)
resolve host name using address family.
Definition resolver.cpp:195
@ Deflate
Definition zstream.hpp:128
@ Gzip
Definition zstream.hpp:130
zlib stream buffer.
Definition zstream.hpp:40
Definition acceptor.hpp:32
constexpr BasicHttpClient< Protocol > & operator>>(BasicHttpClient< Protocol > &in, HttpResponse &response)
read HTTP response from the HTTP stream.
Definition httpclient.hpp:477
std::ostream & operator<<(std::ostream &os, const BasicUnixEndpoint< Protocol > &endpoint)
push endpoint representation into a stream.
Definition endpoint.hpp:255
bool compareNoCase(const std::string &a, const std::string &b)
case insensitive string comparison.
Definition utils.hpp:196
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:283