Breaking changes on the client, push only for keeping the history.

This commit is contained in:
Chunting Gu
2019-03-01 09:29:03 +08:00
parent d42556ce5a
commit b38b7e6d38
12 changed files with 263 additions and 129 deletions
+2
View File
@@ -31,6 +31,8 @@ class HttpAsyncClientBase
explicit HttpAsyncClientBase(boost::asio::io_context& io_context,
std::size_t buffer_size = 0);
virtual ~HttpAsyncClientBase() = default;
WEBCC_DELETE_COPY_ASSIGN(HttpAsyncClientBase);
// Set the timeout seconds for reading response.
+1 -1
View File
@@ -27,7 +27,7 @@ class HttpClientBase {
// 0 means default value (e.g., 1024) will be used.
explicit HttpClientBase(std::size_t buffer_size = 0);
~HttpClientBase() = default;
virtual ~HttpClientBase() = default;
WEBCC_DELETE_COPY_ASSIGN(HttpClientBase);
+6
View File
@@ -58,6 +58,12 @@ void HttpMessage::SetContent(std::string&& content, bool set_length) {
}
}
void HttpMessage::SetContentInAppJsonUtf8(std::string&& content,
bool set_length) {
SetContent(std::move(content), set_length);
SetContentType(http::media_types::kApplicationJson, http::charsets::kUtf8);
}
// ATTENTION: The buffers don't hold the memory!
std::vector<boost::asio::const_buffer> HttpMessage::ToBuffers() const {
assert(!start_line_.empty());
+5
View File
@@ -34,16 +34,21 @@ class HttpMessage {
const std::string& content() const { return content_; }
// TODO: Rename to AddHeader.
void SetHeader(const std::string& name, const std::string& value);
// TODO: Remove
void SetHeader(std::string&& name, std::string&& value);
// E.g., "application/json; charset=utf-8"
void SetContentType(const std::string& media_type,
const std::string& charset);
// TODO: Remove parameter |set_length|.
void SetContent(std::string&& content, bool set_length);
void SetContentInAppJsonUtf8(std::string&& content, bool set_length);
// Make the message (e.g., update start line).
// Must be called before ToBuffers()!
virtual void Prepare() = 0;
+10
View File
@@ -39,6 +39,16 @@ class HttpRequest : public HttpMessage {
return port_.empty() ? default_port : port_;
}
// Shortcut to set `Accept` header.
void Accept(const std::string& media_type) {
SetHeader(http::headers::kAccept, media_type);
}
// Shortcut to set `Accept` header.
void AcceptAppJson() {
SetHeader(http::headers::kAccept, http::media_types::kApplicationJson);
}
// Prepare payload.
// Compose start line, set Host header, etc.
void Prepare() override;
+1 -1
View File
@@ -10,7 +10,7 @@ namespace ssl = boost::asio::ssl;
namespace webcc {
HttpSslClient::HttpSslClient(std::size_t buffer_size, bool ssl_verify)
HttpSslClient::HttpSslClient(bool ssl_verify, std::size_t buffer_size)
: HttpClientBase(buffer_size),
ssl_context_(ssl::context::sslv23),
ssl_socket_(io_context_, ssl_context_),
+1 -1
View File
@@ -13,7 +13,7 @@ class HttpSslClient : public HttpClientBase {
// SSL verification (|ssl_verify|) needs CA certificates to be found
// in the default verify paths of OpenSSL. On Windows, it means you need to
// set environment variable SSL_CERT_FILE properly.
explicit HttpSslClient(std::size_t buffer_size = 0, bool ssl_verify = true);
explicit HttpSslClient(bool ssl_verify = true, std::size_t buffer_size = 0);
~HttpSslClient() = default;
+1
View File
@@ -37,6 +37,7 @@ void RestRequestHandler::HandleSession(HttpSessionPtr session) {
return;
}
// TODO: Let the service to provide the media-type and charset.
RestResponse rest_response;
service->Handle(rest_request, &rest_response);
+29 -7
View File
@@ -1,28 +1,50 @@
#include "webcc/rest_ssl_client.h"
#include "boost/algorithm/string.hpp"
#include "webcc/utility.h"
namespace webcc {
RestClientBase::RestClientBase(HttpClientBase* http_client_base,
const SSMap& headers)
: http_client_base_(http_client_base),
content_media_type_(http::media_types::kApplicationJson),
content_charset_(http::charsets::kUtf8),
headers_(headers) {
}
RestSslClient::RestSslClient(const std::string& host, const std::string& port,
std::size_t buffer_size, bool ssl_verify)
: host_(host), port_(port),
http_client_(buffer_size, ssl_verify) {
bool ssl_verify, const SSMap& headers,
std::size_t buffer_size)
: RestClientBase(&http_ssl_client_, headers),
host_(host), port_(port),
http_ssl_client_(ssl_verify, buffer_size) {
AdjustHostPort(host_, port_);
}
bool RestSslClient::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) {
HttpRequest http_request(method, url, host_, port_);
if (!content.empty()) {
http_request.SetContent(std::move(content), true);
http_request.SetContentType(http::media_types::kApplicationJson,
http::charsets::kUtf8);
http_request.SetContentType(content_media_type_, content_charset_);
}
for (auto& h : headers_) {
http_request.SetHeader(h.first, h.second);
}
for (auto& h : headers) {
http_request.SetHeader(h.first, h.second);
}
http_request.Prepare();
if (!http_client_.Request(http_request, buffer_size)) {
if (!http_ssl_client_.Request(http_request, buffer_size)) {
return false;
}
+80 -45
View File
@@ -2,6 +2,7 @@
#define WEBCC_REST_SSL_CLIENT_H_
#include <cassert>
#include <map>
#include <string>
#include <utility> // for move()
@@ -12,54 +13,30 @@
namespace webcc {
class RestSslClient {
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);
typedef std::map<std::string, std::string> SSMap;
~RestSslClient() = default;
class RestClientBase {
public:
RestClientBase(HttpClientBase* http_client_base,
const SSMap& headers = {});
WEBCC_DELETE_COPY_ASSIGN(RestSslClient);
virtual ~RestClientBase() = default;
WEBCC_DELETE_COPY_ASSIGN(RestClientBase);
void SetTimeout(int seconds) {
http_client_.SetTimeout(seconds);
http_client_base_->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) {
return Request(kHttpGet, url, "", buffer_size);
}
inline bool Post(const std::string& url, std::string&& content,
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,
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,
std::size_t buffer_size = 0) {
return Request(kHttpPatch, url, std::move(content), buffer_size);
}
inline bool Delete(const std::string& url, std::size_t buffer_size = 0) {
return Request(kHttpDelete, url, "", buffer_size);
// Overwrite the default content type (json & utf-8).
void SetContentType(const std::string& media_type,
const std::string& charset) {
content_media_type_ = media_type;
content_charset_ = charset;
}
HttpResponsePtr response() const {
return http_client_.response();
return http_client_base_->response();
}
int response_status() const {
@@ -73,21 +50,79 @@ class RestSslClient {
}
bool timed_out() const {
return http_client_.timed_out();
return http_client_base_->timed_out();
}
Error error() const {
return http_client_.error();
return http_client_base_->error();
}
private:
bool Request(const std::string& method, const std::string& url,
std::string&& content, std::size_t buffer_size);
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:
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,
std::string&& content, const SSMap& headers = {},
std::size_t buffer_size = 0);
private:
std::string host_;
std::string port_;
HttpSslClient http_client_;
HttpSslClient http_ssl_client_;
};
} // namespace webcc