Handle both HTTP & HTTPS in HttpClientSession; remove macro WEBCC_ENABLE_SSL.
This commit is contained in:
@@ -30,6 +30,7 @@ set(HEADERS
|
||||
http_response.h
|
||||
http_response_parser.h
|
||||
http_server.h
|
||||
http_ssl_client.h
|
||||
# http_ssl_async_client.h
|
||||
queue.h
|
||||
url.h
|
||||
@@ -53,6 +54,7 @@ set(SOURCES
|
||||
http_response.cc
|
||||
http_response_parser.cc
|
||||
http_server.cc
|
||||
http_ssl_client.cc
|
||||
# http_ssl_async_client.cc
|
||||
logger.cc
|
||||
url.cc
|
||||
@@ -61,7 +63,7 @@ set(SOURCES
|
||||
|
||||
if(WEBCC_ENABLE_REST)
|
||||
set(REST_HEADERS
|
||||
# rest_async_client.h
|
||||
# rest_async_client.h
|
||||
# rest_client.h
|
||||
rest_request_handler.h
|
||||
rest_server.h
|
||||
@@ -80,11 +82,6 @@ if(WEBCC_ENABLE_REST)
|
||||
set(SOURCES ${SOURCES} ${REST_SOURCES})
|
||||
endif()
|
||||
|
||||
if(WEBCC_ENABLE_SSL)
|
||||
set(HEADERS ${HEADERS} http_ssl_client.h)
|
||||
set(SOURCES ${SOURCES} http_ssl_client.cc)
|
||||
endif()
|
||||
|
||||
if(WEBCC_ENABLE_SOAP)
|
||||
set(SOAP_HEADERS
|
||||
# soap_async_client.h
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "webcc/http_client_session.h"
|
||||
|
||||
#include "webcc/http_client.h"
|
||||
#include "webcc/http_request.h"
|
||||
#include "webcc/http_ssl_client.h"
|
||||
#include "webcc/url.h"
|
||||
|
||||
namespace webcc {
|
||||
@@ -14,7 +14,7 @@ HttpResponsePtr HttpClientSession::Request(HttpRequestArgs&& args) {
|
||||
assert(args.parameters_.size() % 2 == 0);
|
||||
assert(args.headers_.size() % 2 == 0);
|
||||
|
||||
HttpRequest request(args.method_, args.url_, args.parameters_);
|
||||
HttpRequest request{ args.method_, args.url_, args.parameters_ };
|
||||
|
||||
if (!args.data_.empty()) {
|
||||
request.SetContent(std::move(args.data_), true);
|
||||
@@ -39,12 +39,26 @@ HttpResponsePtr HttpClientSession::Request(HttpRequestArgs&& args) {
|
||||
|
||||
request.Prepare();
|
||||
|
||||
HttpClient client;
|
||||
if (!client.Request(request, args.buffer_size_)) {
|
||||
// TODO:
|
||||
|
||||
std::shared_ptr<HttpClientBase> impl;
|
||||
|
||||
if (request.url().scheme() == "http") {
|
||||
impl.reset(new HttpClient);
|
||||
} else if (request.url().scheme() == "https") {
|
||||
impl.reset(new HttpSslClient{args.ssl_verify_});
|
||||
} else {
|
||||
return HttpResponsePtr{};
|
||||
}
|
||||
|
||||
return client.response();
|
||||
if (impl) {
|
||||
if (!impl->Request(request, args.buffer_size_)) {
|
||||
return HttpResponsePtr{};
|
||||
}
|
||||
return impl->response();
|
||||
}
|
||||
|
||||
return HttpResponsePtr{};
|
||||
}
|
||||
|
||||
HttpResponsePtr HttpClientSession::Get(const std::string& url,
|
||||
|
||||
@@ -17,7 +17,7 @@ class HttpClientSession;
|
||||
class HttpRequestArgs {
|
||||
public:
|
||||
explicit HttpRequestArgs(const std::string& method = "")
|
||||
: method_(method), json_(false), buffer_size_(0) {
|
||||
: method_(method), json_(false), ssl_verify_(true), buffer_size_(0) {
|
||||
LOG_VERB("HttpRequestArgs()");
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ public:
|
||||
data_(std::move(rhs.data_)),
|
||||
json_(rhs.json_),
|
||||
headers_(std::move(rhs.headers_)),
|
||||
ssl_verify_(rhs.ssl_verify_),
|
||||
buffer_size_(rhs.buffer_size_) {
|
||||
LOG_VERB("HttpRequestArgs(&&)");
|
||||
}
|
||||
@@ -50,6 +51,7 @@ public:
|
||||
data_ = std::move(rhs.data_);
|
||||
json_ = rhs.json_;
|
||||
headers_ = std::move(rhs.headers_);
|
||||
ssl_verify_ = rhs.ssl_verify_;
|
||||
buffer_size_ = buffer_size_;
|
||||
}
|
||||
LOG_VERB("HttpRequestArgs& operator=(&&)");
|
||||
@@ -108,6 +110,11 @@ public:
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
HttpRequestArgs&& ssl_verify(bool ssl_verify = true) {
|
||||
ssl_verify_ = ssl_verify;
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
HttpRequestArgs&& buffer_size(std::size_t buffer_size) {
|
||||
buffer_size_ = buffer_size;
|
||||
return std::move(*this);
|
||||
@@ -130,6 +137,10 @@ private:
|
||||
|
||||
std::vector<std::string> headers_;
|
||||
|
||||
// Verify the certificate of the peer (remote server) or not.
|
||||
// HTTPS only.
|
||||
bool ssl_verify_;
|
||||
|
||||
// Size of the buffer to read response.
|
||||
// Leave it to 0 for using default value.
|
||||
std::size_t buffer_size_;
|
||||
|
||||
Reference in New Issue
Block a user