Refine client session and its examples.

This commit is contained in:
Chunting Gu
2019-03-13 13:46:22 +08:00
parent 5abd7e275c
commit 05782b773a
7 changed files with 166 additions and 167 deletions
+4
View File
@@ -139,6 +139,10 @@ public:
return msg_.c_str();
}
Error error() const { return error_; }
bool timeout() const { return timeout_; }
private:
Error error_;
+6
View File
@@ -25,10 +25,16 @@ HttpClientPtr HttpClientPool::Get(const Key& key) const {
void HttpClientPool::Add(const Key& key, HttpClientPtr client) {
clients_[key] = client;
LOG_INFO("Added connection to pool (%s, %s, %s).",
key.scheme.c_str(), key.host.c_str(), key.port.c_str());
}
void HttpClientPool::Remove(const Key& key) {
clients_.erase(key);
LOG_INFO("Removed connection from pool (%s, %s, %s).",
key.scheme.c_str(), key.host.c_str(), key.port.c_str());
}
} // namespace webcc
+20 -16
View File
@@ -43,35 +43,39 @@ HttpResponsePtr HttpClientSession::Request(HttpRequestArgs&& args) {
request.Prepare();
// Determine SSL verify flag.
bool ssl_verify = true;
if (args.ssl_verify_) {
ssl_verify = args.ssl_verify_.value();
} else if (ssl_verify_) {
ssl_verify = ssl_verify_.value();
}
bool reuse = false;
const HttpClientPool::Key key{ request.url() };
bool new_created = false;
HttpClientPtr client = pool_.Get(key);
if (!client) {
new_created = true;
client.reset(new HttpClient{ 0, args.ssl_verify_ });
client.reset(new HttpClient{ 0, ssl_verify });
reuse = false;
} else {
new_created = false;
// TODO: Apply args.ssl_verify even if reuse a client.
reuse = false;
LOG_VERB("Reuse an existing connection.");
}
if (!client->Request(request, args.buffer_size_, new_created)) {
if (!client->Request(request, args.buffer_size_, !reuse)) {
throw Exception(client->error(), client->timed_out());
}
if (new_created) {
if (!client->closed()) {
pool_.Add(key, client);
LOG_VERB("Added connection to the pool (%s, %s, %s).",
key.scheme.c_str(), key.host.c_str(), key.port.c_str());
}
} else {
// Update pool.
if (reuse) {
if (client->closed()) {
pool_.Remove(key);
LOG_VERB("Removed connection from the pool (%s, %s, %s).",
key.scheme.c_str(), key.host.c_str(), key.port.c_str());
}
} else {
if (!client->closed()) {
pool_.Add(key, client);
}
}
+10
View File
@@ -5,6 +5,8 @@
#include <string>
#include <vector>
#include "boost/optional.hpp"
#include "webcc/http_client_pool.h"
#include "webcc/http_request_args.h"
#include "webcc/http_response.h"
@@ -29,6 +31,10 @@ public:
headers_.Add(key, value);
}
void set_ssl_verify(bool ssl_verify) {
ssl_verify_.emplace(ssl_verify);
}
HttpResponsePtr Request(HttpRequestArgs&& args);
HttpResponsePtr Get(const std::string& url,
@@ -43,6 +49,7 @@ public:
private:
void InitHeaders();
private:
// E.g., "application/json".
std::string content_type_;
@@ -52,6 +59,9 @@ private:
// Headers for each request sent from this session.
HttpHeaderDict headers_;
// Verify the certificate of the peer or not.
boost::optional<bool> ssl_verify_;
// Connection pool for keep-alive.
HttpClientPool pool_;
};
+8 -5
View File
@@ -5,6 +5,8 @@
#include <utility>
#include <vector>
#include "boost/optional.hpp"
#include "webcc/globals.h"
#include "webcc/logger.h"
@@ -17,7 +19,7 @@ class HttpClientSession;
class HttpRequestArgs {
public:
explicit HttpRequestArgs(const std::string& method = "")
: method_(method), json_(false), ssl_verify_(true), buffer_size_(0) {
: method_(method), json_(false), buffer_size_(0) {
LOG_VERB("HttpRequestArgs()");
}
@@ -78,7 +80,7 @@ public:
}
HttpRequestArgs&& ssl_verify(bool ssl_verify = true) {
ssl_verify_ = ssl_verify;
ssl_verify_.emplace(ssl_verify);
return std::move(*this);
}
@@ -94,6 +96,7 @@ private:
std::string url_;
// URL query parameters.
std::vector<std::string> parameters_;
// Data to send in the body of the request.
@@ -102,11 +105,11 @@ private:
// Is the data to send a JSON string?
bool json_;
// Additional request headers.
std::vector<std::string> headers_;
// Verify the certificate of the peer (remote server) or not.
// HTTPS only.
bool ssl_verify_;
// Verify the certificate of the peer or not.
boost::optional<bool> ssl_verify_;
// Size of the buffer to read response.
// Leave it to 0 for using default value.