25#ifndef __JOIN_HTTP_CLIENT_HPP__
26#define __JOIN_HTTP_CLIENT_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)
323 this->
_keepTimeout = std::chrono::seconds (::atoi (alive.substr (pos + 8, alive.find (
",", pos + 8)).c_str ()));
326 pos = alive.find (
"max=");
327 if (pos != std::string::npos)
329 this->
_keepMax = ::atoi (alive.substr (pos + 4, alive.find (
",", pos + 4)).c_str ());
339 if (response.
hasHeader (
"Transfer-Encoding"))
343 if (response.
hasHeader (
"Content-Encoding"))
349 this->
_timestamp = std::chrono::steady_clock::now ();
361 for (
auto const& encoding : encodings)
363 if (encoding.find (
"gzip") != std::string::npos)
368 else if (encoding.find (
"deflate") != std::string::npos)
373 else if (encoding.find (
"chunked") != std::string::npos)
406 auto duration = std::chrono::steady_clock::now () - this->
_timestamp;
407 return (this->
_keepTimeout < std::chrono::duration_cast <std::chrono::seconds> (duration)) || (this->
_keepMax == 0);
416 return !this->connected () || this->
expired ();
427 this->connect (endpoint);
437 std::chrono::time_point <std::chrono::steady_clock>
_timestamp;
461 template <
class Protocol>
474 template <
class Protocol>
484 template <
class Protocol>
541 BasicHttpClient <Protocol>::operator= (std::move (other));
568 this->connectEncrypted (endpoint);
578 template <
class Protocol>
579 constexpr BasicHttpSecureClient <Protocol>&
operator<< (BasicHttpSecureClient <Protocol>& out,
HttpRequest& request)
591 template <
class Protocol>
basic HTTP client.
Definition httpclient.hpp:46
bool _keep
HTTP keep alive.
Definition httpclient.hpp:446
bool _wrapped
HTTP stream status.
Definition httpclient.hpp:434
std::string url() const
get URL.
Definition httpclient.hpp:193
std::chrono::time_point< std::chrono::steady_clock > _timestamp
HTTP timestamp.
Definition httpclient.hpp:437
std::string _host
HTTP host.
Definition httpclient.hpp:440
uint16_t _port
HTTP port.
Definition httpclient.hpp:443
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:452
void setEncoding(const std::vector< std::string > &encodings)
set stream encoding.
Definition httpclient.hpp:359
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:423
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:449
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:386
virtual std::string scheme() const
get HTTP scheme.
Definition httpclient.hpp:141
bool needReconnection()
check if client must reconnect.
Definition httpclient.hpp:414
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:404
std::streambuf * _streambuf
HTTP stream buffer.
Definition httpclient.hpp:431
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:486
std::string scheme() const override
get HTTP scheme.
Definition httpclient.hpp:554
void reconnect(const Endpoint &endpoint) override
perform reconnection to the given endpoint.
Definition httpclient.hpp:564
BasicHttpSecureClient(const char *host, uint16_t port=443, bool keepAlive=true)
create the basic HTTPS client instance.
Definition httpclient.hpp:496
BasicHttpSecureClient(BasicHttpSecureClient &&other)
create the basic HTTPS client instance by move.
Definition httpclient.hpp:529
BasicHttpSecureClient(const std::string &host, uint16_t port=443, bool keepAlive=true)
create the basic HTTPS client instance.
Definition httpclient.hpp:507
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:488
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:338
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:544
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:546
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:194
@ 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:475
std::ostream & operator<<(std::ostream &os, const BasicUnixEndpoint< Protocol > &endpoint)
push endpoint representation into a stream.
Definition endpoint.hpp:255
__inline__ 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:274
__inline__ bool compareNoCase(const std::string &a, const std::string &b)
case insensitive string comparison.
Definition utils.hpp:188