|
|
@ -2,6 +2,7 @@
|
|
|
|
#define WEBCC_REST_SSL_CLIENT_H_
|
|
|
|
#define WEBCC_REST_SSL_CLIENT_H_
|
|
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
#include <string>
|
|
|
|
#include <utility> // for move()
|
|
|
|
#include <utility> // for move()
|
|
|
|
|
|
|
|
|
|
|
@ -12,54 +13,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
namespace webcc {
|
|
|
|
namespace webcc {
|
|
|
|
|
|
|
|
|
|
|
|
class RestSslClient {
|
|
|
|
typedef std::map<std::string, std::string> SSMap;
|
|
|
|
public:
|
|
|
|
|
|
|
|
// If |port| is empty, |host| will be checked to see if it contains port or
|
|
|
|
|
|
|
|
// not (separated by ':').
|
|
|
|
|
|
|
|
explicit RestSslClient(const std::string& host,
|
|
|
|
|
|
|
|
const std::string& port = "",
|
|
|
|
|
|
|
|
std::size_t buffer_size = 0,
|
|
|
|
|
|
|
|
bool ssl_verify = true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
~RestSslClient() = default;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WEBCC_DELETE_COPY_ASSIGN(RestSslClient);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SetTimeout(int seconds) {
|
|
|
|
|
|
|
|
http_client_.SetTimeout(seconds);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE:
|
|
|
|
|
|
|
|
// The return value of the following methods (Get, Post, etc.) only indicates
|
|
|
|
|
|
|
|
// if the socket communication is successful or not. Check error() and
|
|
|
|
|
|
|
|
// timed_out() for more information if it's failed. Check response_status()
|
|
|
|
|
|
|
|
// instead for the HTTP status code.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Get(const std::string& url, std::size_t buffer_size = 0) {
|
|
|
|
class RestClientBase {
|
|
|
|
return Request(kHttpGet, url, "", buffer_size);
|
|
|
|
public:
|
|
|
|
}
|
|
|
|
RestClientBase(HttpClientBase* http_client_base,
|
|
|
|
|
|
|
|
const SSMap& headers = {});
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Post(const std::string& url, std::string&& content,
|
|
|
|
virtual ~RestClientBase() = default;
|
|
|
|
std::size_t buffer_size = 0) {
|
|
|
|
|
|
|
|
return Request(kHttpPost, url, std::move(content), buffer_size);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Put(const std::string& url, std::string&& content,
|
|
|
|
WEBCC_DELETE_COPY_ASSIGN(RestClientBase);
|
|
|
|
std::size_t buffer_size = 0) {
|
|
|
|
|
|
|
|
return Request(kHttpPut, url, std::move(content), buffer_size);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Patch(const std::string& url, std::string&& content,
|
|
|
|
void SetTimeout(int seconds) {
|
|
|
|
std::size_t buffer_size = 0) {
|
|
|
|
http_client_base_->SetTimeout(seconds);
|
|
|
|
return Request(kHttpPatch, url, std::move(content), buffer_size);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Delete(const std::string& url, std::size_t buffer_size = 0) {
|
|
|
|
// Overwrite the default content type (json & utf-8).
|
|
|
|
return Request(kHttpDelete, url, "", buffer_size);
|
|
|
|
void SetContentType(const std::string& media_type,
|
|
|
|
|
|
|
|
const std::string& charset) {
|
|
|
|
|
|
|
|
content_media_type_ = media_type;
|
|
|
|
|
|
|
|
content_charset_ = charset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HttpResponsePtr response() const {
|
|
|
|
HttpResponsePtr response() const {
|
|
|
|
return http_client_.response();
|
|
|
|
return http_client_base_->response();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int response_status() const {
|
|
|
|
int response_status() const {
|
|
|
@ -73,21 +50,79 @@ class RestSslClient {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool timed_out() const {
|
|
|
|
bool timed_out() const {
|
|
|
|
return http_client_.timed_out();
|
|
|
|
return http_client_base_->timed_out();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Error error() const {
|
|
|
|
Error error() const {
|
|
|
|
return http_client_.error();
|
|
|
|
return http_client_base_->error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
// Default: "application/json".
|
|
|
|
|
|
|
|
std::string content_media_type_;
|
|
|
|
|
|
|
|
// Default: "utf-8".
|
|
|
|
|
|
|
|
std::string content_charset_;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Default headers for all session requests.
|
|
|
|
|
|
|
|
std::map<std::string, std::string> headers_;
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
private:
|
|
|
|
|
|
|
|
HttpClientBase* http_client_base_;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RestSslClient : public RestClientBase {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
// If |port| is empty, |host| will be checked to see if it contains port or
|
|
|
|
|
|
|
|
// not (separated by ':').
|
|
|
|
|
|
|
|
explicit RestSslClient(const std::string& host,
|
|
|
|
|
|
|
|
const std::string& port = "",
|
|
|
|
|
|
|
|
bool ssl_verify = true,
|
|
|
|
|
|
|
|
const SSMap& headers = {},
|
|
|
|
|
|
|
|
std::size_t buffer_size = 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
~RestSslClient() = default;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE:
|
|
|
|
|
|
|
|
// The return value of the following methods (Get, Post, etc.) only indicates
|
|
|
|
|
|
|
|
// if the socket communication is successful or not. Check error() and
|
|
|
|
|
|
|
|
// timed_out() for more information if it's failed. Check response_status()
|
|
|
|
|
|
|
|
// instead for the HTTP status code.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Get(const std::string& url, const SSMap& headers = {},
|
|
|
|
|
|
|
|
std::size_t buffer_size = 0) {
|
|
|
|
|
|
|
|
return Request(kHttpGet, url, "", headers, buffer_size);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Post(const std::string& url, std::string&& content,
|
|
|
|
|
|
|
|
const SSMap& headers = {}, std::size_t buffer_size = 0) {
|
|
|
|
|
|
|
|
return Request(kHttpPost, url, std::move(content), headers, buffer_size);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Put(const std::string& url, std::string&& content,
|
|
|
|
|
|
|
|
const SSMap& headers = {}, std::size_t buffer_size = 0) {
|
|
|
|
|
|
|
|
return Request(kHttpPut, url, std::move(content), headers, buffer_size);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Patch(const std::string& url, std::string&& content,
|
|
|
|
|
|
|
|
const SSMap& headers = {}, std::size_t buffer_size = 0) {
|
|
|
|
|
|
|
|
return Request(kHttpPatch, url, std::move(content), headers, buffer_size);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline bool Delete(const std::string& url, const SSMap& headers = {},
|
|
|
|
|
|
|
|
std::size_t buffer_size = 0) {
|
|
|
|
|
|
|
|
return Request(kHttpDelete, url, "", headers, buffer_size);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Request(const std::string& method, const std::string& url,
|
|
|
|
bool Request(const std::string& method, const std::string& url,
|
|
|
|
std::string&& content, std::size_t buffer_size);
|
|
|
|
std::string&& content, const SSMap& headers = {},
|
|
|
|
|
|
|
|
std::size_t buffer_size = 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string host_;
|
|
|
|
std::string host_;
|
|
|
|
std::string port_;
|
|
|
|
std::string port_;
|
|
|
|
|
|
|
|
|
|
|
|
HttpSslClient http_client_;
|
|
|
|
HttpSslClient http_ssl_client_;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace webcc
|
|
|
|
} // namespace webcc
|
|
|
|