Allow to set buffer size for client; don't auto adjust reading buffer size; refine the global definitions.

This commit is contained in:
Chunting Gu
2019-02-11 12:46:27 +08:00
parent ae9d4efcf1
commit 3e60c76da1
47 changed files with 559 additions and 423 deletions
+38 -3
View File
@@ -5,6 +5,11 @@
#include "webcc/rest_ssl_client.h"
#include "webcc/logger.h"
const bool kSslVerify = false;
#define PRINT_CONTENT 0
static Json::Value StringToJson(const std::string& str) {
Json::Value json;
@@ -18,10 +23,11 @@ static Json::Value StringToJson(const std::string& str) {
return json;
}
void Test() {
webcc::RestSslClient client("api.github.com");
void ListPublicEvents() {
webcc::RestSslClient client("api.github.com", "", kSslVerify);
if (client.Get("/events")) {
#if PRINT_CONTENT
Json::Value json = StringToJson(client.response()->content());
// Pretty print the JSON.
@@ -33,6 +39,33 @@ void Test() {
writer->write(json, &std::cout);
std::cout << std::endl;
#endif // PRINT_CONTENT
} else {
std::cout << webcc::DescribeError(client.error());
if (client.timed_out()) {
std::cout << " (timed out)";
}
std::cout << std::endl;
}
}
void ListUserFollowers() {
webcc::RestSslClient client("api.github.com", "", kSslVerify);
if (client.Get("/users/sprinfall/followers")) {
#if PRINT_CONTENT
Json::Value json = StringToJson(client.response()->content());
// Pretty print the JSON.
Json::StreamWriterBuilder builder;
builder["indentation"] = " ";
std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
writer->write(json, &std::cout);
std::cout << std::endl;
#endif // PRINT_CONTENT
} else {
std::cout << webcc::DescribeError(client.error());
if (client.timed_out()) {
@@ -45,7 +78,9 @@ void Test() {
int main() {
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
Test();
//ListPublicEvents();
ListUserFollowers();
return 0;
}
+1 -4
View File
@@ -37,10 +37,7 @@
#include "webcc/logger.h"
void Test() {
webcc::HttpRequest request;
request.set_method(webcc::kHttpGet);
request.set_url("/get");
request.set_host("httpbin.org"/*, "80"*/);
webcc::HttpRequest request(webcc::kHttpGet, "/get", "httpbin.org"/*, "80"*/);
request.Make();
webcc::HttpClient client;
+2 -4
View File
@@ -11,11 +11,9 @@
// The default port number should be 8000.
void Test(boost::asio::io_context& io_context) {
webcc::HttpRequestPtr request(new webcc::HttpRequest());
webcc::HttpRequestPtr request = std::make_shared<webcc::HttpRequest>(
webcc::kHttpGet, "/index.html", "localhost", "8000");
request->set_method(webcc::kHttpGet);
request->set_url("/index.html");
request->set_host("localhost", "8000");
request->Make();
webcc::HttpAsyncClientPtr client(new webcc::HttpAsyncClient(io_context));
+2 -4
View File
@@ -9,10 +9,8 @@
// The default port number should be 8000.
void Test() {
webcc::HttpRequest request;
request.set_method(webcc::kHttpGet);
request.set_url("/index.html");
request.set_host("localhost", "8000");
webcc::HttpRequest request(webcc::kHttpGet, "/index.html", "localhost",
"8000");
request.Make();
webcc::HttpClient client;
+5 -6
View File
@@ -21,20 +21,19 @@ int main(int argc, char* argv[]) {
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
webcc::HttpRequest request;
request.set_method(webcc::kHttpGet);
request.set_url(url);
request.set_host(host); // Leave port to default value.
// Leave port to default value.
webcc::HttpRequest request(webcc::kHttpGet, url, host);
request.Make();
// Verify the certificate of the peer or not.
// See HttpSslClient::Request() for more details.
bool ssl_verify = false;
webcc::HttpSslClient client(ssl_verify);
webcc::HttpSslClient client(2000, ssl_verify);
if (client.Request(request)) {
std::cout << client.response()->content() << std::endl;
//std::cout << client.response()->content() << std::endl;
} else {
std::cout << webcc::DescribeError(client.error());
if (client.timed_out()) {
+9 -9
View File
@@ -16,7 +16,7 @@ class BookClientBase {
const std::string& host, const std::string& port,
int timeout_seconds)
: rest_client_(io_context, host, port) {
rest_client_.set_timeout_seconds(timeout_seconds);
rest_client_.SetTimeout(timeout_seconds);
}
virtual ~BookClientBase() = default;
@@ -58,10 +58,10 @@ class BookListClient : public BookClientBase {
: BookClientBase(io_context, host, port, timeout_seconds) {
}
void ListBooks(webcc::HttpResponseHandler handler) {
void ListBooks(webcc::HttpResponseCallback callback) {
std::cout << "ListBooks" << std::endl;
rest_client_.Get("/books", handler);
rest_client_.Get("/books", callback);
}
void CreateBook(const std::string& title, double price,
@@ -96,7 +96,7 @@ class BookDetailClient : public BookClientBase {
: BookClientBase(io_context, host, port, timeout_seconds) {
}
void GetBook(const std::string& id, webcc::HttpResponseHandler handler) {
void GetBook(const std::string& id, webcc::HttpResponseCallback callback) {
std::cout << "GetBook: " << id << std::endl;
auto rsp_callback = [](webcc::HttpResponsePtr response) {
@@ -105,13 +105,13 @@ class BookDetailClient : public BookClientBase {
//id_callback(rsp_json["id"].asString());
};
rest_client_.Get("/books/" + id, handler);
rest_client_.Get("/books/" + id, callback);
}
void UpdateBook(const std::string& id,
const std::string& title,
double price,
webcc::HttpResponseHandler handler) {
webcc::HttpResponseCallback callback) {
std::cout << "UpdateBook: " << id << " " << title << " " << price
<< std::endl;
@@ -120,13 +120,13 @@ class BookDetailClient : public BookClientBase {
json["title"] = title;
json["price"] = price;
rest_client_.Put("/books/" + id, JsonToString(json), handler);
rest_client_.Put("/books/" + id, JsonToString(json), callback);
}
void DeleteBook(const std::string& id, webcc::HttpResponseHandler handler) {
void DeleteBook(const std::string& id, webcc::HttpResponseCallback callback) {
std::cout << "DeleteBook: " << id << std::endl;
rest_client_.Delete("/books/" + id, handler);
rest_client_.Delete("/books/" + id, callback);
}
};
+6 -6
View File
@@ -40,7 +40,7 @@ class BookClientBase {
}
// Check HTTP response status.
bool CheckStatus(webcc::HttpStatus::Enum expected_status) {
bool CheckStatus(webcc::http::Status expected_status) {
int status = rest_client_.response_status();
if (status != expected_status) {
LOG_ERRO("HTTP status error (actual: %d, expected: %d).",
@@ -69,7 +69,7 @@ class BookListClient : public BookClientBase {
return false;
}
if (!CheckStatus(webcc::HttpStatus::kOK)) {
if (!CheckStatus(webcc::http::Status::kOK)) {
// Response HTTP status error.
return false;
}
@@ -97,7 +97,7 @@ class BookListClient : public BookClientBase {
return false;
}
if (!CheckStatus(webcc::HttpStatus::kCreated)) {
if (!CheckStatus(webcc::http::Status::kCreated)) {
return false;
}
@@ -123,7 +123,7 @@ class BookDetailClient : public BookClientBase {
return false;
}
if (!CheckStatus(webcc::HttpStatus::kOK)) {
if (!CheckStatus(webcc::http::Status::kOK)) {
return false;
}
@@ -141,7 +141,7 @@ class BookDetailClient : public BookClientBase {
return false;
}
if (!CheckStatus(webcc::HttpStatus::kOK)) {
if (!CheckStatus(webcc::http::Status::kOK)) {
return false;
}
@@ -154,7 +154,7 @@ class BookDetailClient : public BookClientBase {
return false;
}
if (!CheckStatus(webcc::HttpStatus::kOK)) {
if (!CheckStatus(webcc::http::Status::kOK)) {
return false;
}
+12 -12
View File
@@ -35,7 +35,7 @@ void BookListService::Get(const webcc::UrlQuery& /*query*/,
}
response->content = JsonToString(json);
response->status = webcc::HttpStatus::kOK;
response->status = webcc::http::Status::kOK;
}
void BookListService::Post(const std::string& request_content,
@@ -50,10 +50,10 @@ void BookListService::Post(const std::string& request_content,
json["id"] = id;
response->content = JsonToString(json);
response->status = webcc::HttpStatus::kCreated;
response->status = webcc::http::Status::kCreated;
} else {
// Invalid JSON
response->status = webcc::HttpStatus::kBadRequest;
response->status = webcc::http::Status::kBadRequest;
}
}
@@ -67,7 +67,7 @@ void BookDetailService::Get(const webcc::UrlSubMatches& url_sub_matches,
if (url_sub_matches.size() != 1) {
// Using kNotFound means the resource specified by the URL cannot be found.
// kBadRequest could be another choice.
response->status = webcc::HttpStatus::kNotFound;
response->status = webcc::http::Status::kNotFound;
return;
}
@@ -75,12 +75,12 @@ void BookDetailService::Get(const webcc::UrlSubMatches& url_sub_matches,
const Book& book = g_book_store.GetBook(book_id);
if (book.IsNull()) {
response->status = webcc::HttpStatus::kNotFound;
response->status = webcc::http::Status::kNotFound;
return;
}
response->content = BookToJsonString(book);
response->status = webcc::HttpStatus::kOK;
response->status = webcc::http::Status::kOK;
}
void BookDetailService::Put(const webcc::UrlSubMatches& url_sub_matches,
@@ -89,7 +89,7 @@ void BookDetailService::Put(const webcc::UrlSubMatches& url_sub_matches,
Sleep(sleep_seconds_);
if (url_sub_matches.size() != 1) {
response->status = webcc::HttpStatus::kNotFound;
response->status = webcc::http::Status::kNotFound;
return;
}
@@ -97,14 +97,14 @@ void BookDetailService::Put(const webcc::UrlSubMatches& url_sub_matches,
Book book;
if (!JsonStringToBook(request_content, &book)) {
response->status = webcc::HttpStatus::kBadRequest;
response->status = webcc::http::Status::kBadRequest;
return;
}
book.id = book_id;
g_book_store.UpdateBook(book);
response->status = webcc::HttpStatus::kOK;
response->status = webcc::http::Status::kOK;
}
void BookDetailService::Delete(const webcc::UrlSubMatches& url_sub_matches,
@@ -112,16 +112,16 @@ void BookDetailService::Delete(const webcc::UrlSubMatches& url_sub_matches,
Sleep(sleep_seconds_);
if (url_sub_matches.size() != 1) {
response->status = webcc::HttpStatus::kNotFound;
response->status = webcc::http::Status::kNotFound;
return;
}
const std::string& book_id = url_sub_matches[0];
if (!g_book_store.DeleteBook(book_id)) {
response->status = webcc::HttpStatus::kNotFound;
response->status = webcc::http::Status::kNotFound;
return;
}
response->status = webcc::HttpStatus::kOK;
response->status = webcc::http::Status::kOK;
}
+1 -1
View File
@@ -104,7 +104,7 @@ bool BookClient::Call1(const std::string& operation,
bool BookClient::Call(const std::string& operation,
std::vector<webcc::SoapParameter>&& parameters,
std::string* result_str) {
if (!soap_client_.Request(operation, std::move(parameters), kResult,
if (!soap_client_.Request(operation, std::move(parameters), kResult, 0,
result_str)) {
PrintError();
return false;
+1 -1
View File
@@ -55,7 +55,7 @@ class CalcClient {
};
std::string result_str;
if (!soap_client_.Request(operation, std::move(parameters), kResultName,
if (!soap_client_.Request(operation, std::move(parameters), kResultName, 0,
&result_str)) {
PrintError();
return false;
+1 -1
View File
@@ -56,7 +56,7 @@ class CalcClient {
};
std::string result_str;
if (!soap_client_.Request(operation, std::move(parameters), kResultName,
if (!soap_client_.Request(operation, std::move(parameters), kResultName, 0,
&result_str)) {
PrintError();
return false;