Add async-client support; refine http message dump format.
This commit is contained in:
+18
-10
@@ -4,8 +4,7 @@ project(webcc)
|
||||
option(WEBCC_ENABLE_LOG "Enable console logger?" ON)
|
||||
option(WEBCC_ENABLE_SOAP "Enable SOAP support (need pugixml)?" ON)
|
||||
option(WEBCC_BUILD_UNITTEST "Build unit test?" ON)
|
||||
option(WEBCC_BUILD_REST_EXAMPLE "Build REST example?" ON)
|
||||
option(WEBCC_BUILD_SOAP_EXAMPLE "Build SOAP example?" ON)
|
||||
option(WEBCC_BUILD_EXAMPLE "Build examples?" ON)
|
||||
|
||||
if(WEBCC_ENABLE_LOG)
|
||||
add_definitions(-DWEBCC_ENABLE_LOG)
|
||||
@@ -45,7 +44,12 @@ if(WIN32)
|
||||
message(STATUS "_WIN32_WINNT=${ver}")
|
||||
# Asio needs this!
|
||||
add_definitions(-D_WIN32_WINNT=${ver})
|
||||
endif(WIN32)
|
||||
endif()
|
||||
|
||||
if(WIN32)
|
||||
# Disable warning on boost string algorithms.
|
||||
add_definitions(-D_SCL_SECURE_NO_WARNINGS)
|
||||
endif()
|
||||
|
||||
# Group sources by dir.
|
||||
# Usage: source_group_by_dir(SRCS)
|
||||
@@ -104,18 +108,22 @@ endif()
|
||||
|
||||
add_subdirectory(src/webcc)
|
||||
|
||||
if(WEBCC_BUILD_REST_EXAMPLE)
|
||||
if(WEBCC_BUILD_EXAMPLE)
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/example/http/client)
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/example/http/async_client)
|
||||
|
||||
# REST example needs jsoncpp to parse and create JSON.
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/jsoncpp)
|
||||
include_directories(${PROJECT_SOURCE_DIR}/third_party/jsoncpp)
|
||||
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/example/rest_book_server)
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/example/rest_book_client)
|
||||
endif()
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/example/rest/book_server)
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/example/rest/book_client)
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/example/rest/book_async_client)
|
||||
|
||||
if(WEBCC_BUILD_SOAP_EXAMPLE)
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/example/soap_calc_server)
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/example/soap_calc_client)
|
||||
if(WEBCC_ENABLE_SOAP)
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/example/soap/calc_server)
|
||||
add_subdirectory(${PROJECT_SOURCE_DIR}/example/soap/calc_client)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(WEBCC_BUILD_UNITTEST)
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
add_executable(http_async_client main.cc)
|
||||
|
||||
target_link_libraries(http_async_client webcc ${Boost_LIBRARIES})
|
||||
target_link_libraries(http_async_client "${CMAKE_THREAD_LIBS_INIT}")
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "boost/asio/io_context.hpp"
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/http_async_client.h"
|
||||
|
||||
// In order to test this client, create a file index.html whose content is
|
||||
// simply "Hello, World!", then start a HTTP server with Python 3:
|
||||
// $ python -m http.server
|
||||
// The default port number should be 8000.
|
||||
|
||||
void Test(boost::asio::io_context& ioc) {
|
||||
std::shared_ptr<webcc::HttpRequest> request(new webcc::HttpRequest());
|
||||
|
||||
request->set_method(webcc::kHttpGet);
|
||||
request->set_url("/index.html");
|
||||
request->SetHost("localhost", "8000");
|
||||
|
||||
request->Build();
|
||||
|
||||
webcc::HttpAsyncClientPtr client(new webcc::HttpAsyncClient(ioc));
|
||||
|
||||
// Response handler.
|
||||
auto handler = [](std::shared_ptr<webcc::HttpResponse> response,
|
||||
webcc::Error error) {
|
||||
if (error == webcc::kNoError) {
|
||||
std::cout << response->content() << std::endl;
|
||||
} else {
|
||||
std::cout << webcc::DescribeError(error) << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
client->Request(request, handler);
|
||||
}
|
||||
|
||||
int main() {
|
||||
LOG_INIT(webcc::ERRO, 0);
|
||||
|
||||
boost::asio::io_context ioc;
|
||||
|
||||
Test(ioc);
|
||||
Test(ioc);
|
||||
Test(ioc);
|
||||
|
||||
ioc.run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
add_executable(http_client main.cc)
|
||||
|
||||
target_link_libraries(http_client webcc ${Boost_LIBRARIES})
|
||||
target_link_libraries(http_client "${CMAKE_THREAD_LIBS_INIT}")
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/http_client.h"
|
||||
|
||||
// In order to test this client, create a file index.html whose content is
|
||||
// simply "Hello, World!", then start a HTTP server with Python 3:
|
||||
// $ python -m http.server
|
||||
// The default port number should be 8000.
|
||||
|
||||
void Test() {
|
||||
webcc::HttpRequest request;
|
||||
|
||||
request.set_method(webcc::kHttpGet);
|
||||
request.set_url("/index.html");
|
||||
request.SetHost("localhost", "8000");
|
||||
|
||||
request.Build();
|
||||
|
||||
webcc::HttpResponse response;
|
||||
|
||||
webcc::HttpClient client;
|
||||
if (!client.Request(request)) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::cout << response.content() << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
LOG_INIT(webcc::ERRO, 0);
|
||||
|
||||
Test();
|
||||
Test();
|
||||
Test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
add_executable(rest_book_async_client main.cc)
|
||||
|
||||
target_link_libraries(rest_book_async_client webcc jsoncpp ${Boost_LIBRARIES})
|
||||
target_link_libraries(rest_book_async_client "${CMAKE_THREAD_LIBS_INIT}")
|
||||
@@ -0,0 +1,137 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "json/json.h"
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/rest_async_client.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Write a JSON object to string.
|
||||
std::string JsonToString(const Json::Value& json) {
|
||||
Json::StreamWriterBuilder builder;
|
||||
return Json::writeString(builder, json);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class BookListClient {
|
||||
public:
|
||||
BookListClient(boost::asio::io_context& io_context,
|
||||
const std::string& host, const std::string& port)
|
||||
: client_(io_context, host, port) {
|
||||
}
|
||||
|
||||
void ListBooks(webcc::HttpResponseHandler handler) {
|
||||
std::cout << "ListBooks" << std::endl;
|
||||
|
||||
client_.Get("/books", handler);
|
||||
}
|
||||
|
||||
void CreateBook(const std::string& id,
|
||||
const std::string& title,
|
||||
double price,
|
||||
webcc::HttpResponseHandler handler) {
|
||||
std::cout << "CreateBook: " << id << " " << title << " " << price
|
||||
<< std::endl;
|
||||
|
||||
Json::Value json(Json::objectValue);
|
||||
json["id"] = id;
|
||||
json["title"] = title;
|
||||
json["price"] = price;
|
||||
|
||||
client_.Post("/books", JsonToString(json), handler);
|
||||
}
|
||||
|
||||
private:
|
||||
webcc::RestAsyncClient client_;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class BookDetailClient {
|
||||
public:
|
||||
BookDetailClient(boost::asio::io_context& io_context,
|
||||
const std::string& host, const std::string& port)
|
||||
: rest_client_(io_context, host, port) {
|
||||
}
|
||||
|
||||
void GetBook(const std::string& id, webcc::HttpResponseHandler handler) {
|
||||
std::cout << "GetBook: " << id << std::endl;
|
||||
|
||||
rest_client_.Get("/book/" + id, handler);
|
||||
}
|
||||
|
||||
void UpdateBook(const std::string& id,
|
||||
const std::string& title,
|
||||
double price,
|
||||
webcc::HttpResponseHandler handler) {
|
||||
std::cout << "UpdateBook: " << id << " " << title << " " << price
|
||||
<< std::endl;
|
||||
|
||||
// NOTE: ID is already in the URL.
|
||||
Json::Value json(Json::objectValue);
|
||||
json["title"] = title;
|
||||
json["price"] = price;
|
||||
|
||||
rest_client_.Put("/book/" + id, JsonToString(json), handler);
|
||||
}
|
||||
|
||||
void DeleteBook(const std::string& id, webcc::HttpResponseHandler handler) {
|
||||
std::cout << "DeleteBook: " << id << std::endl;
|
||||
|
||||
rest_client_.Delete("/book/" + id, handler);
|
||||
}
|
||||
|
||||
private:
|
||||
webcc::RestAsyncClient rest_client_;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
void Help(const char* argv0) {
|
||||
std::cout << "Usage: " << argv0 << " <host> <port>" << std::endl;
|
||||
std::cout << " E.g.," << std::endl;
|
||||
std::cout << " " << argv0 << " localhost 8080" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 3) {
|
||||
Help(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
LOG_INIT(webcc::ERRO, 0);
|
||||
|
||||
std::string host = argv[1];
|
||||
std::string port = argv[2];
|
||||
|
||||
boost::asio::io_context io_context;
|
||||
|
||||
BookListClient list_client(io_context, host, port);
|
||||
BookDetailClient detail_client(io_context, host, port);
|
||||
|
||||
// Response handler.
|
||||
auto handler = [](std::shared_ptr<webcc::HttpResponse> response,
|
||||
webcc::Error error) {
|
||||
if (error == webcc::kNoError) {
|
||||
std::cout << response->content() << std::endl;
|
||||
} else {
|
||||
std::cout << webcc::DescribeError(error) << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
list_client.ListBooks(handler);
|
||||
list_client.CreateBook("1", "1984", 12.3, handler);
|
||||
|
||||
detail_client.GetBook("1", handler);
|
||||
detail_client.UpdateBook("1", "1Q84", 32.1, handler);
|
||||
detail_client.GetBook("1", handler);
|
||||
detail_client.DeleteBook("1", handler);
|
||||
|
||||
list_client.ListBooks(handler);
|
||||
|
||||
io_context.run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,12 +1,8 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "boost/algorithm/string.hpp"
|
||||
#include "json/json.h"
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/http_client.h"
|
||||
#include "webcc/http_request.h"
|
||||
#include "webcc/http_response.h"
|
||||
#include "webcc/rest_client.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -21,26 +17,28 @@ std::string JsonToString(const Json::Value& json) {
|
||||
|
||||
class BookListClient {
|
||||
public:
|
||||
BookListClient(const std::string& host, const std::string& port)
|
||||
: rest_client_(host, port) {
|
||||
BookListClient(const std::string& host, const std::string& port,
|
||||
int timeout_seconds)
|
||||
: client_(host, port) {
|
||||
client_.set_timeout_seconds(timeout_seconds);
|
||||
}
|
||||
|
||||
bool ListBooks() {
|
||||
std::cout << "ListBooks" << std::endl;
|
||||
|
||||
webcc::HttpResponse http_response;
|
||||
if (!rest_client_.Get("/books", &http_response)) {
|
||||
if (!client_.Get("/books")) {
|
||||
std::cout << webcc::DescribeError(client_.error()) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << http_response.content() << std::endl;
|
||||
std::cout << client_.response_content() << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CreateBook(const std::string& id,
|
||||
const std::string& title,
|
||||
double price) {
|
||||
std::cout << "CreateBook: " << id << " " << title << " " << price
|
||||
std::cout << "CreateBook: " << id << ", " << title << ", " << price
|
||||
<< std::endl;
|
||||
|
||||
Json::Value json(Json::objectValue);
|
||||
@@ -48,44 +46,46 @@ public:
|
||||
json["title"] = title;
|
||||
json["price"] = price;
|
||||
|
||||
webcc::HttpResponse http_response;
|
||||
if (!rest_client_.Post("/books", JsonToString(json), &http_response)) {
|
||||
if (!client_.Post("/books", JsonToString(json))) {
|
||||
std::cout << webcc::DescribeError(client_.error()) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << http_response.status() << std::endl;
|
||||
std::cout << client_.response_status() << std::endl;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private:
|
||||
webcc::RestClient rest_client_;
|
||||
webcc::RestClient client_;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
class BookDetailClient {
|
||||
public:
|
||||
BookDetailClient(const std::string& host, const std::string& port)
|
||||
BookDetailClient(const std::string& host, const std::string& port,
|
||||
int timeout_seconds)
|
||||
: rest_client_(host, port) {
|
||||
rest_client_.set_timeout_seconds(timeout_seconds);
|
||||
}
|
||||
|
||||
bool GetBook(const std::string& id) {
|
||||
std::cout << "GetBook: " << id << std::endl;
|
||||
|
||||
webcc::HttpResponse http_response;
|
||||
if (!rest_client_.Get("/book/" + id, &http_response)) {
|
||||
if (!rest_client_.Get("/book/" + id)) {
|
||||
std::cout << webcc::DescribeError(rest_client_.error()) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << http_response.content() << std::endl;
|
||||
std::cout << rest_client_.response_content() << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool UpdateBook(const std::string& id,
|
||||
const std::string& title,
|
||||
double price) {
|
||||
std::cout << "UpdateBook: " << id << " " << title << " " << price
|
||||
std::cout << "UpdateBook: " << id << ", " << title << ", " << price
|
||||
<< std::endl;
|
||||
|
||||
// NOTE: ID is already in the URL.
|
||||
@@ -93,24 +93,24 @@ public:
|
||||
json["title"] = title;
|
||||
json["price"] = price;
|
||||
|
||||
webcc::HttpResponse http_response;
|
||||
if (!rest_client_.Put("/book/" + id, JsonToString(json), &http_response)) {
|
||||
if (!rest_client_.Put("/book/" + id, JsonToString(json))) {
|
||||
std::cout << webcc::DescribeError(rest_client_.error()) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << http_response.status() << std::endl;
|
||||
std::cout << rest_client_.response_status() << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DeleteBook(const std::string& id) {
|
||||
std::cout << "DeleteBook: " << id << std::endl;
|
||||
|
||||
webcc::HttpResponse http_response;
|
||||
if (!rest_client_.Delete("/book/" + id, &http_response)) {
|
||||
if (!rest_client_.Delete("/book/" + id)) {
|
||||
std::cout << webcc::DescribeError(rest_client_.error()) << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << http_response.status() << std::endl;
|
||||
std::cout << rest_client_.response_status() << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -121,24 +121,30 @@ private:
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
void Help(const char* argv0) {
|
||||
std::cout << "Usage: " << argv0 << " <host> <port>" << std::endl;
|
||||
std::cout << "Usage: " << argv0 << " <host> <port> [timeout]" << std::endl;
|
||||
std::cout << " E.g.," << std::endl;
|
||||
std::cout << " " << argv0 << " localhost 8080" << std::endl;
|
||||
std::cout << " " << argv0 << " localhost 8080 2" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 3) {
|
||||
if (argc < 3) {
|
||||
Help(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
LOG_INIT(webcc::ERRO, 0);
|
||||
LOG_INIT(webcc::VERB, 0);
|
||||
|
||||
std::string host = argv[1];
|
||||
std::string port = argv[2];
|
||||
|
||||
BookListClient list_client(host, port);
|
||||
BookDetailClient detail_client(host, port);
|
||||
int timeout_seconds = -1;
|
||||
if (argc > 3) {
|
||||
timeout_seconds = std::atoi(argv[3]);
|
||||
}
|
||||
|
||||
BookListClient list_client(host, port, timeout_seconds);
|
||||
BookDetailClient detail_client(host, port, timeout_seconds);
|
||||
|
||||
list_client.ListBooks();
|
||||
list_client.CreateBook("1", "1984", 12.3);
|
||||
@@ -4,7 +4,9 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "boost/lexical_cast.hpp"
|
||||
#include "boost/thread/thread.hpp"
|
||||
#include "json/json.h"
|
||||
#include "webcc/logger.h"
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@@ -121,6 +123,11 @@ static bool BookFromJson(const std::string& json, Book* book) {
|
||||
// TODO: Support query parameters.
|
||||
bool BookListService::Get(const webcc::UrlQuery& /* query */,
|
||||
std::string* response_content) {
|
||||
if (sleep_seconds_ > 0) {
|
||||
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
|
||||
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
|
||||
}
|
||||
|
||||
Json::Value root(Json::arrayValue);
|
||||
for (const Book& book : g_book_store.books()) {
|
||||
root.append(book.ToJson());
|
||||
@@ -136,10 +143,16 @@ bool BookListService::Get(const webcc::UrlQuery& /* query */,
|
||||
// No response content.
|
||||
bool BookListService::Post(const std::string& request_content,
|
||||
std::string* /* response_content */) {
|
||||
if (sleep_seconds_ > 0) {
|
||||
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
|
||||
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
|
||||
}
|
||||
|
||||
Book book;
|
||||
if (BookFromJson(request_content, &book)) {
|
||||
return g_book_store.AddBook(book);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -148,6 +161,11 @@ bool BookListService::Post(const std::string& request_content,
|
||||
bool BookDetailService::Get(const std::vector<std::string>& url_sub_matches,
|
||||
const webcc::UrlQuery& query,
|
||||
std::string* response_content) {
|
||||
if (sleep_seconds_ > 0) {
|
||||
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
|
||||
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
|
||||
}
|
||||
|
||||
if (url_sub_matches.size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -168,6 +186,11 @@ bool BookDetailService::Get(const std::vector<std::string>& url_sub_matches,
|
||||
bool BookDetailService::Put(const std::vector<std::string>& url_sub_matches,
|
||||
const std::string& request_content,
|
||||
std::string* response_content) {
|
||||
if (sleep_seconds_ > 0) {
|
||||
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
|
||||
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
|
||||
}
|
||||
|
||||
if (url_sub_matches.size() != 1) {
|
||||
return false;
|
||||
}
|
||||
@@ -185,6 +208,11 @@ bool BookDetailService::Put(const std::vector<std::string>& url_sub_matches,
|
||||
|
||||
bool BookDetailService::Delete(
|
||||
const std::vector<std::string>& url_sub_matches) {
|
||||
if (sleep_seconds_ > 0) {
|
||||
LOG_INFO("Sleep %d seconds...", sleep_seconds_);
|
||||
boost::this_thread::sleep_for(boost::chrono::seconds(sleep_seconds_));
|
||||
}
|
||||
|
||||
if (url_sub_matches.size() != 1) {
|
||||
return false;
|
||||
}
|
||||
+21
-5
@@ -12,17 +12,25 @@
|
||||
// - /books?name={BookName}
|
||||
// The query parameters could be regular expressions.
|
||||
class BookListService : public webcc::RestListService {
|
||||
public:
|
||||
BookListService(int sleep_seconds) : sleep_seconds_(sleep_seconds) {
|
||||
}
|
||||
|
||||
protected:
|
||||
// Return a list of books based on query parameters.
|
||||
// URL examples:
|
||||
// - /books
|
||||
// - /books?name={BookName}
|
||||
bool Get(const webcc::UrlQuery& query,
|
||||
std::string* response_content) final;
|
||||
std::string* response_content) override;
|
||||
|
||||
// Create a new book.
|
||||
bool Post(const std::string& request_content,
|
||||
std::string* response_content) final;
|
||||
std::string* response_content) override;
|
||||
|
||||
private:
|
||||
// Sleep for the client to test timeout control.
|
||||
int sleep_seconds_ = 0;
|
||||
};
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -30,16 +38,24 @@ class BookListService : public webcc::RestListService {
|
||||
// The URL is like '/books/{BookID}', and the 'url_sub_matches' parameter
|
||||
// contains the matched book ID.
|
||||
class BookDetailService : public webcc::RestDetailService {
|
||||
public:
|
||||
BookDetailService(int sleep_seconds) : sleep_seconds_(sleep_seconds) {
|
||||
}
|
||||
|
||||
protected:
|
||||
bool Get(const std::vector<std::string>& url_sub_matches,
|
||||
const webcc::UrlQuery& query,
|
||||
std::string* response_content) final;
|
||||
std::string* response_content) override;
|
||||
|
||||
bool Put(const std::vector<std::string>& url_sub_matches,
|
||||
const std::string& request_content,
|
||||
std::string* response_content) final;
|
||||
std::string* response_content) override;
|
||||
|
||||
bool Delete(const std::vector<std::string>& url_sub_matches) final;
|
||||
bool Delete(const std::vector<std::string>& url_sub_matches) override;
|
||||
|
||||
private:
|
||||
// Sleep for the client to test timeout control.
|
||||
int sleep_seconds_ = 0;
|
||||
};
|
||||
|
||||
#endif // BOOK_SERVICE_H_
|
||||
@@ -0,0 +1,52 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/rest_server.h"
|
||||
|
||||
#include "book_services.h"
|
||||
|
||||
void Help(const char* argv0) {
|
||||
std::cout << "Usage: " << argv0 << " <port> [seconds]" << std::endl;
|
||||
std::cout << "If |seconds| is provided, the server will sleep these seconds "
|
||||
"before sending back each response."
|
||||
<< std::endl;
|
||||
std::cout << " E.g.," << std::endl;
|
||||
std::cout << " " << argv0 << " 8080" << std::endl;
|
||||
std::cout << " " << argv0 << " 8080 3" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 2) {
|
||||
Help(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
LOG_INIT(webcc::VERB, 0);
|
||||
|
||||
unsigned short port = std::atoi(argv[1]);
|
||||
|
||||
int sleep_seconds = 0;
|
||||
if (argc >= 3) {
|
||||
sleep_seconds = std::atoi(argv[2]);
|
||||
}
|
||||
|
||||
std::size_t workers = 2;
|
||||
|
||||
try {
|
||||
webcc::RestServer server(port, workers);
|
||||
|
||||
server.Bind(std::make_shared<BookListService>(sleep_seconds),
|
||||
"/books", false);
|
||||
|
||||
server.Bind(std::make_shared<BookDetailService>(sleep_seconds),
|
||||
"/book/(\\d+)", true);
|
||||
|
||||
server.Run();
|
||||
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "Exception: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/rest_server.h"
|
||||
|
||||
#include "book_services.h"
|
||||
|
||||
static void Help(const char* argv0) {
|
||||
std::cout << "Usage: " << argv0 << " <port>" << std::endl;
|
||||
std::cout << " E.g.," << std::endl;
|
||||
std::cout << " " << argv0 << " 8080" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 2) {
|
||||
Help(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
LOG_INIT(webcc::VERB, 0);
|
||||
|
||||
unsigned short port = std::atoi(argv[1]);
|
||||
|
||||
std::size_t workers = 2;
|
||||
|
||||
try {
|
||||
webcc::RestServer server(port, workers);
|
||||
|
||||
server.Bind(std::make_shared<BookListService>(), "/books", false);
|
||||
|
||||
server.Bind(std::make_shared<BookDetailService>(), "/book/(\\d+)", true);
|
||||
|
||||
server.Run();
|
||||
|
||||
} catch (std::exception& e) {
|
||||
std::cerr << "Exception: " << e.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,7 +1,3 @@
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
file(GLOB SRCS *.cc *.h)
|
||||
|
||||
add_executable(soap_calc_client ${SRCS})
|
||||
@@ -74,7 +74,7 @@ bool CalcClient::Calc(const std::string& operation,
|
||||
if (error != webcc::kNoError) {
|
||||
LOG_ERRO("Operation '%s' failed: %s",
|
||||
operation.c_str(),
|
||||
webcc::GetErrorMessage(error));
|
||||
webcc::DescribeError(error));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
file(GLOB SRCS *.cc *.h)
|
||||
|
||||
add_executable(soap_calc_server ${SRCS})
|
||||
@@ -1,6 +1,8 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/soap_server.h"
|
||||
|
||||
#include "calc_service.h"
|
||||
|
||||
static void Help(const char* argv0) {
|
||||
@@ -4,6 +4,8 @@ add_definitions(-DBOOST_ASIO_NO_DEPRECATED)
|
||||
set(SRCS
|
||||
globals.cc
|
||||
globals.h
|
||||
http_async_client.cc
|
||||
http_async_client.h
|
||||
http_client.cc
|
||||
http_client.h
|
||||
http_message.cc
|
||||
@@ -27,6 +29,8 @@ set(SRCS
|
||||
logger.cc
|
||||
logger.h
|
||||
queue.h
|
||||
rest_async_client.cc
|
||||
rest_async_client.h
|
||||
rest_client.cc
|
||||
rest_client.h
|
||||
rest_request_handler.cc
|
||||
|
||||
@@ -31,7 +31,7 @@ const std::string kHttpDelete = "DELETE";
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
const char* GetErrorMessage(Error error) {
|
||||
const char* DescribeError(Error error) {
|
||||
switch (error) {
|
||||
case kHostResolveError:
|
||||
return "Cannot resolve the host.";
|
||||
|
||||
+9
-27
@@ -4,38 +4,15 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace webcc {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Macros
|
||||
|
||||
// Explicitly declare the assignment operator as deleted.
|
||||
#define DISALLOW_ASSIGN(TypeName) TypeName& operator=(const TypeName&) = delete;
|
||||
|
||||
// Explicitly declare the copy constructor and assignment operator as deleted.
|
||||
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
|
||||
#define DELETE_COPY_AND_ASSIGN(TypeName) \
|
||||
TypeName(const TypeName&) = delete; \
|
||||
DISALLOW_ASSIGN(TypeName)
|
||||
TypeName& operator=(const TypeName&) = delete;
|
||||
|
||||
// Explicitly declare all implicit constructors as deleted, namely the
|
||||
// default constructor, copy constructor and operator= functions.
|
||||
// This is especially useful for classes containing only static methods.
|
||||
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
|
||||
TypeName() = delete; \
|
||||
DISALLOW_COPY_AND_ASSIGN(TypeName)
|
||||
|
||||
// Disallow copying a type, but provide default construction, move construction
|
||||
// and move assignment. Especially useful for move-only structs.
|
||||
#define MOVE_ONLY_WITH_DEFAULT_CONSTRUCTORS(TypeName) \
|
||||
TypeName() = default; \
|
||||
MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(TypeName)
|
||||
|
||||
// Disallow copying a type, and only provide move construction and move
|
||||
// assignment. Especially useful for move-only structs.
|
||||
#define MOVE_ONLY_NO_DEFAULT_CONSTRUCTOR(TypeName) \
|
||||
TypeName(TypeName&&) = default; \
|
||||
TypeName& operator=(TypeName&&) = default; \
|
||||
DISALLOW_COPY_AND_ASSIGN(TypeName)
|
||||
namespace webcc {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
// Constants
|
||||
@@ -46,6 +23,11 @@ const std::size_t kBufferSize = 1024;
|
||||
|
||||
const std::size_t kInvalidLength = static_cast<std::size_t>(-1);
|
||||
|
||||
// Timeout seconds.
|
||||
const int kMaxConnectSeconds = 10;
|
||||
const int kMaxSendSeconds = 10;
|
||||
const int kMaxReceiveSeconds = 30;
|
||||
|
||||
extern const std::string kContentType;
|
||||
extern const std::string kContentLength;
|
||||
extern const std::string kSoapAction;
|
||||
@@ -107,7 +89,7 @@ enum Error {
|
||||
};
|
||||
|
||||
// Return a descriptive message for the given error code.
|
||||
const char* GetErrorMessage(Error error);
|
||||
const char* DescribeError(Error error);
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -1,41 +1,42 @@
|
||||
#include "webcc/http_async_client.h"
|
||||
|
||||
#if 0
|
||||
#include "boost/asio.hpp"
|
||||
#else
|
||||
#include "boost/asio/connect.hpp"
|
||||
#include "boost/asio/read.hpp"
|
||||
#include "boost/asio/write.hpp"
|
||||
#endif
|
||||
|
||||
#include "webcc/http_response_parser.h"
|
||||
#include "webcc/http_request.h"
|
||||
#include "webcc/http_response.h"
|
||||
|
||||
using boost::asio::ip::tcp;
|
||||
#include "webcc/logger.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
HttpAsyncClient::HttpAsyncClient(boost::asio::io_context& io_context)
|
||||
: socket_(io_context) {
|
||||
|
||||
: socket_(io_context),
|
||||
timeout_seconds_(kMaxReceiveSeconds),
|
||||
deadline_timer_(io_context) {
|
||||
resolver_.reset(new tcp::resolver(io_context));
|
||||
|
||||
response_.reset(new HttpResponse());
|
||||
parser_.reset(new HttpResponseParser(response_.get()));
|
||||
response_parser_.reset(new HttpResponseParser(response_.get()));
|
||||
|
||||
deadline_timer_.expires_at(boost::posix_time::pos_infin);
|
||||
|
||||
// Start the persistent actor that checks for deadline expiry.
|
||||
CheckDeadline();
|
||||
}
|
||||
|
||||
Error HttpAsyncClient::SendRequest(std::shared_ptr<HttpRequest> request,
|
||||
Error HttpAsyncClient::Request(std::shared_ptr<HttpRequest> request,
|
||||
HttpResponseHandler response_handler) {
|
||||
assert(request);
|
||||
assert(response_handler);
|
||||
|
||||
request_ = request;
|
||||
response_handler_ = response_handler;
|
||||
|
||||
std::string port = request->port();
|
||||
if (port.empty()) {
|
||||
port = "80";
|
||||
}
|
||||
|
||||
auto handler = std::bind(&HttpAsyncClient::HandleResolve,
|
||||
this,
|
||||
auto handler = std::bind(&HttpAsyncClient::ResolveHandler,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2);
|
||||
|
||||
@@ -44,110 +45,113 @@ Error HttpAsyncClient::SendRequest(std::shared_ptr<HttpRequest> request,
|
||||
return kNoError;
|
||||
}
|
||||
|
||||
void HttpAsyncClient::HandleResolve(boost::system::error_code ec,
|
||||
void HttpAsyncClient::ResolveHandler(boost::system::error_code ec,
|
||||
tcp::resolver::results_type results) {
|
||||
if (ec) {
|
||||
std::cerr << "Resolve: " << ec.message() << std::endl;
|
||||
// return kHostResolveError;
|
||||
LOG_ERRO("Can't resolve host (%s): %s, %s", ec.message().c_str(),
|
||||
request_->host().c_str(), request_->port().c_str());
|
||||
response_handler_(response_, kHostResolveError);
|
||||
} else {
|
||||
endpoints_ = results;
|
||||
DoConnect(endpoints_.begin());
|
||||
AsyncConnect(endpoints_.begin());
|
||||
}
|
||||
}
|
||||
|
||||
void HttpAsyncClient::DoConnect(tcp::resolver::results_type::iterator endpoint_it) {
|
||||
void HttpAsyncClient::AsyncConnect(tcp::resolver::results_type::iterator endpoint_it) {
|
||||
if (endpoint_it != endpoints_.end()) {
|
||||
deadline_timer_.expires_from_now(
|
||||
boost::posix_time::seconds(kMaxConnectSeconds));
|
||||
|
||||
socket_.async_connect(endpoint_it->endpoint(),
|
||||
std::bind(&HttpAsyncClient::HandleConnect,
|
||||
this,
|
||||
std::bind(&HttpAsyncClient::ConnectHandler,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
endpoint_it));
|
||||
}
|
||||
}
|
||||
|
||||
void HttpAsyncClient::HandleConnect(boost::system::error_code ec,
|
||||
void HttpAsyncClient::ConnectHandler(
|
||||
boost::system::error_code ec,
|
||||
tcp::resolver::results_type::iterator endpoint_it) {
|
||||
if (ec) {
|
||||
// Will be here if the end point is v6.
|
||||
std::cout << "Connect error: " << ec.message() << std::endl;
|
||||
// return kEndpointConnectError;
|
||||
|
||||
// Will be here if the endpoint is IPv6.
|
||||
response_handler_(response_, kEndpointConnectError);
|
||||
socket_.close();
|
||||
|
||||
// Try the next available endpoint.
|
||||
DoConnect(++endpoint_it);
|
||||
AsyncConnect(++endpoint_it);
|
||||
} else {
|
||||
DoWrite();
|
||||
AsyncWrite();
|
||||
}
|
||||
}
|
||||
|
||||
// Send HTTP request.
|
||||
void HttpAsyncClient::DoWrite() {
|
||||
void HttpAsyncClient::AsyncWrite() {
|
||||
deadline_timer_.expires_from_now(boost::posix_time::seconds(kMaxSendSeconds));
|
||||
|
||||
boost::asio::async_write(socket_,
|
||||
request_->ToBuffers(),
|
||||
std::bind(&HttpAsyncClient::HandleWrite,
|
||||
this,
|
||||
std::bind(&HttpAsyncClient::WriteHandler,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1));
|
||||
}
|
||||
|
||||
void HttpAsyncClient::HandleWrite(boost::system::error_code ec) {
|
||||
void HttpAsyncClient::WriteHandler(boost::system::error_code ec) {
|
||||
if (ec) {
|
||||
//return kSocketWriteError;
|
||||
return;
|
||||
response_handler_(response_, kSocketWriteError);
|
||||
} else {
|
||||
AsyncRead();
|
||||
}
|
||||
}
|
||||
|
||||
DoRead();
|
||||
}
|
||||
void HttpAsyncClient::AsyncRead() {
|
||||
deadline_timer_.expires_from_now(
|
||||
boost::posix_time::seconds(timeout_seconds_));
|
||||
|
||||
void HttpAsyncClient::DoRead() {
|
||||
socket_.async_read_some(boost::asio::buffer(buffer_),
|
||||
std::bind(&HttpAsyncClient::HandleRead,
|
||||
this,
|
||||
std::bind(&HttpAsyncClient::ReadHandler,
|
||||
shared_from_this(),
|
||||
std::placeholders::_1,
|
||||
std::placeholders::_2));
|
||||
}
|
||||
|
||||
void HttpAsyncClient::HandleRead(boost::system::error_code ec,
|
||||
void HttpAsyncClient::ReadHandler(boost::system::error_code ec,
|
||||
std::size_t length) {
|
||||
if (ec || length == 0) {
|
||||
//return kSocketReadError;
|
||||
response_handler_(response_, kSocketReadError);
|
||||
return;
|
||||
}
|
||||
|
||||
// Parse the response piece just read.
|
||||
// If the content has been fully received, next time flag "finished_"
|
||||
// will be set.
|
||||
Error error = parser_->Parse(buffer_.data(), length);
|
||||
// If the content has been fully received, |finished()| will be true.
|
||||
Error error = response_parser_->Parse(buffer_.data(), length);
|
||||
|
||||
if (error != kNoError) {
|
||||
//return error;
|
||||
}
|
||||
|
||||
if (parser_->finished()) {
|
||||
response_handler_(response_, error);
|
||||
return;
|
||||
}
|
||||
|
||||
// Read and parse HTTP response.
|
||||
if (response_parser_->finished()) {
|
||||
response_handler_(response_, error);
|
||||
return;
|
||||
}
|
||||
|
||||
// NOTE:
|
||||
// We must stop trying to read once all content has been received,
|
||||
// because some servers will block extra call to read_some().
|
||||
//while (!parser_.finished()) {
|
||||
// size_t length = socket_.read_some(boost::asio::buffer(buffer_), ec);
|
||||
AsyncRead();
|
||||
}
|
||||
|
||||
//if (length == 0 || ec) {
|
||||
// return kSocketReadError;
|
||||
//}
|
||||
void HttpAsyncClient::CheckDeadline() {
|
||||
if (deadline_timer_.expires_at() <=
|
||||
boost::asio::deadline_timer::traits_type::now()) {
|
||||
// The deadline has passed.
|
||||
// The socket is closed so that any outstanding asynchronous operations
|
||||
// are canceled.
|
||||
boost::system::error_code ignored_ec;
|
||||
socket_.close(ignored_ec);
|
||||
|
||||
// Parse the response piece just read.
|
||||
// If the content has been fully received, next time flag "finished_"
|
||||
// will be set.
|
||||
//Error error = parser_.Parse(buffer_.data(), length);
|
||||
deadline_timer_.expires_at(boost::posix_time::pos_infin);
|
||||
}
|
||||
|
||||
//if (error != kNoError) {
|
||||
// return error;
|
||||
//}
|
||||
//}
|
||||
// Put the actor back to sleep.
|
||||
deadline_timer_.async_wait(std::bind(&HttpAsyncClient::CheckDeadline,
|
||||
shared_from_this()));
|
||||
}
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
@@ -2,61 +2,79 @@
|
||||
#define WEBCC_HTTP_ASYNC_CLIENT_H_
|
||||
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#include "boost/smart_ptr/scoped_ptr.hpp"
|
||||
|
||||
#include "boost/asio/deadline_timer.hpp"
|
||||
#include "boost/asio/io_context.hpp"
|
||||
#include "boost/asio/ip/tcp.hpp"
|
||||
|
||||
#include "webcc/common.h"
|
||||
#include "webcc/globals.h"
|
||||
#include "webcc/http_request.h"
|
||||
#include "webcc/http_response.h"
|
||||
#include "webcc/http_response_parser.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
class HttpRequest;
|
||||
class HttpResponse;
|
||||
typedef std::function<void(HttpResponsePtr, Error)> HttpResponseHandler;
|
||||
|
||||
typedef void(*HttpResponseHandler)(std::shared_ptr<HttpResponse>);
|
||||
|
||||
class HttpAsyncClient {
|
||||
class HttpAsyncClient : public std::enable_shared_from_this<HttpAsyncClient> {
|
||||
public:
|
||||
HttpAsyncClient(boost::asio::io_context& io_context);
|
||||
explicit HttpAsyncClient(boost::asio::io_context& io_context);
|
||||
|
||||
Error SendRequest(std::shared_ptr<HttpRequest> request,
|
||||
HttpResponseHandler response_handler);
|
||||
DELETE_COPY_AND_ASSIGN(HttpAsyncClient);
|
||||
|
||||
void set_timeout_seconds(int timeout_seconds) {
|
||||
timeout_seconds_ = timeout_seconds;
|
||||
}
|
||||
|
||||
// Asynchronously connect to the server, send the request, read the response,
|
||||
// and call the |response_handler| when all these finish.
|
||||
Error Request(HttpRequestPtr request, HttpResponseHandler response_handler);
|
||||
|
||||
private:
|
||||
void HandleResolve(boost::system::error_code ec,
|
||||
boost::asio::ip::tcp::resolver::results_type results);
|
||||
using tcp = boost::asio::ip::tcp;
|
||||
|
||||
void DoConnect(boost::asio::ip::tcp::resolver::results_type::iterator endpoint_it);
|
||||
void ResolveHandler(boost::system::error_code ec,
|
||||
tcp::resolver::results_type results);
|
||||
|
||||
void HandleConnect(boost::system::error_code ec,
|
||||
boost::asio::ip::tcp::resolver::results_type::iterator endpoint_it);
|
||||
void AsyncConnect(tcp::resolver::results_type::iterator endpoint_it);
|
||||
|
||||
void DoWrite();
|
||||
void ConnectHandler(boost::system::error_code ec,
|
||||
tcp::resolver::results_type::iterator endpoint_it);
|
||||
|
||||
void HandleWrite(boost::system::error_code ec);
|
||||
void AsyncWrite();
|
||||
void WriteHandler(boost::system::error_code ec);
|
||||
|
||||
void DoRead();
|
||||
void AsyncRead();
|
||||
void ReadHandler(boost::system::error_code ec, std::size_t length);
|
||||
|
||||
void HandleRead(boost::system::error_code ec, std::size_t length);
|
||||
void CheckDeadline();
|
||||
|
||||
private:
|
||||
boost::asio::ip::tcp::socket socket_;
|
||||
tcp::socket socket_;
|
||||
|
||||
std::shared_ptr<HttpRequest> request_;
|
||||
|
||||
std::unique_ptr<boost::asio::ip::tcp::resolver> resolver_;
|
||||
boost::asio::ip::tcp::resolver::results_type endpoints_;
|
||||
std::unique_ptr<tcp::resolver> resolver_;
|
||||
tcp::resolver::results_type endpoints_;
|
||||
|
||||
std::array<char, kBufferSize> buffer_;
|
||||
|
||||
std::unique_ptr<HttpResponseParser> parser_;
|
||||
std::unique_ptr<HttpResponseParser> response_parser_;
|
||||
|
||||
std::shared_ptr<HttpResponse> response_;
|
||||
HttpResponsePtr response_;
|
||||
HttpResponseHandler response_handler_;
|
||||
|
||||
// Maximum seconds to wait before the client cancels the operation.
|
||||
// Only for receiving response from server.
|
||||
int timeout_seconds_;
|
||||
|
||||
// Timer for the timeout control.
|
||||
boost::asio::deadline_timer deadline_timer_;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<HttpAsyncClient> HttpAsyncClientPtr;
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
#endif // WEBCC_HTTP_ASYNC_CLIENT_H_
|
||||
|
||||
+34
-44
@@ -19,43 +19,33 @@
|
||||
|
||||
namespace webcc {
|
||||
|
||||
static const int kConnectMaxSeconds = 10;
|
||||
static const int kSendMaxSeconds = 10;
|
||||
static const int kReceiveMaxSeconds = 30;
|
||||
|
||||
HttpClient::HttpClient()
|
||||
: socket_(io_context_),
|
||||
timeout_seconds_(kReceiveMaxSeconds),
|
||||
timeout_seconds_(kMaxReceiveSeconds),
|
||||
deadline_timer_(io_context_) {
|
||||
deadline_timer_.expires_at(boost::posix_time::pos_infin);
|
||||
|
||||
response_.reset(new HttpResponse());
|
||||
response_parser_.reset(new HttpResponseParser(response_.get()));
|
||||
|
||||
// Start the persistent actor that checks for deadline expiry.
|
||||
CheckDeadline();
|
||||
}
|
||||
|
||||
Error HttpClient::Request(const HttpRequest& request, HttpResponse* response) {
|
||||
assert(response != nullptr);
|
||||
|
||||
Error error = kNoError;
|
||||
|
||||
if ((error = Connect(request)) != kNoError) {
|
||||
return error;
|
||||
bool HttpClient::Request(const HttpRequest& request) {
|
||||
if ((error_ = Connect(request)) != kNoError) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Send HTTP request.
|
||||
|
||||
if ((error = SendReqeust(request)) != kNoError) {
|
||||
return error;
|
||||
if ((error_ = SendReqeust(request)) != kNoError) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Read and parse HTTP response.
|
||||
if ((error_ = ReadResponse()) != kNoError) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// NOTE: Don't use make_unique because it's since C++14.
|
||||
parser_.reset(new HttpResponseParser(response));
|
||||
|
||||
error = ReadResponse(response);
|
||||
|
||||
return error;
|
||||
return true;
|
||||
}
|
||||
|
||||
Error HttpClient::Connect(const HttpRequest& request) {
|
||||
@@ -72,15 +62,13 @@ Error HttpClient::Connect(const HttpRequest& request) {
|
||||
auto endpoints = resolver.resolve(tcp::v4(), request.host(), port, ec);
|
||||
|
||||
if (ec) {
|
||||
LOG_ERRO("cannot resolve host: %s, %s",
|
||||
request.host().c_str(),
|
||||
port.c_str());
|
||||
|
||||
LOG_ERRO("Can't resolve host (%s): %s, %s", ec.message().c_str(),
|
||||
request.host().c_str(), port.c_str());
|
||||
return kHostResolveError;
|
||||
}
|
||||
|
||||
deadline_timer_.expires_from_now(
|
||||
boost::posix_time::seconds(kConnectMaxSeconds));
|
||||
boost::posix_time::seconds(kMaxConnectSeconds));
|
||||
|
||||
ec = boost::asio::error::would_block;
|
||||
|
||||
@@ -110,9 +98,9 @@ Error HttpClient::Connect(const HttpRequest& request) {
|
||||
}
|
||||
|
||||
Error HttpClient::SendReqeust(const HttpRequest& request) {
|
||||
LOG_VERB("http request:\n{\n%s}", request.Dump().c_str());
|
||||
LOG_VERB("HTTP request:\n%s", request.Dump(4, "> ").c_str());
|
||||
|
||||
deadline_timer_.expires_from_now(boost::posix_time::seconds(kSendMaxSeconds));
|
||||
deadline_timer_.expires_from_now(boost::posix_time::seconds(kMaxSendSeconds));
|
||||
|
||||
boost::system::error_code ec = boost::asio::error::would_block;
|
||||
|
||||
@@ -132,7 +120,7 @@ Error HttpClient::SendReqeust(const HttpRequest& request) {
|
||||
return kNoError;
|
||||
}
|
||||
|
||||
Error HttpClient::ReadResponse(HttpResponse* response) {
|
||||
Error HttpClient::ReadResponse() {
|
||||
deadline_timer_.expires_from_now(
|
||||
boost::posix_time::seconds(timeout_seconds_));
|
||||
|
||||
@@ -141,31 +129,33 @@ Error HttpClient::ReadResponse(HttpResponse* response) {
|
||||
|
||||
socket_.async_read_some(
|
||||
boost::asio::buffer(buffer_),
|
||||
[this, &ec, &error, response](boost::system::error_code inner_ec,
|
||||
[this, &ec, &error](boost::system::error_code inner_ec,
|
||||
std::size_t length) {
|
||||
ec = inner_ec;
|
||||
|
||||
if (inner_ec || length == 0) {
|
||||
LOG_ERRO("Socket read error.");
|
||||
error = kSocketReadError;
|
||||
} else {
|
||||
// Parse the response piece just read.
|
||||
// If the content has been fully received, next time flag "finished_"
|
||||
// will be set.
|
||||
error = parser_->Parse(buffer_.data(), length);
|
||||
|
||||
if (error != kNoError) {
|
||||
LOG_ERRO("failed to parse http response.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (parser_->finished()) {
|
||||
// Parse the response piece just read.
|
||||
// If the content has been fully received, next time flag "finished_"
|
||||
// will be set.
|
||||
error = response_parser_->Parse(buffer_.data(), length);
|
||||
|
||||
if (error != kNoError) {
|
||||
LOG_ERRO("Failed to parse HTTP response.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (response_parser_->finished()) {
|
||||
// Stop trying to read once all content has been received,
|
||||
// because some servers will block extra call to read_some().
|
||||
return;
|
||||
}
|
||||
|
||||
ReadResponse(response);
|
||||
}
|
||||
ReadResponse();
|
||||
});
|
||||
|
||||
// Block until the asynchronous operation has completed.
|
||||
@@ -174,7 +164,7 @@ Error HttpClient::ReadResponse(HttpResponse* response) {
|
||||
} while (ec == boost::asio::error::would_block);
|
||||
|
||||
if (error == kNoError) {
|
||||
LOG_VERB("http response:\n{\n%s}", response->Dump().c_str());
|
||||
LOG_VERB("HTTP response:\n%s", response_->Dump(4, "> ").c_str());
|
||||
}
|
||||
|
||||
return error;
|
||||
|
||||
+16
-10
@@ -9,32 +9,37 @@
|
||||
#include "boost/asio/ip/tcp.hpp"
|
||||
|
||||
#include "webcc/globals.h"
|
||||
#include "webcc/http_request.h"
|
||||
#include "webcc/http_response.h"
|
||||
#include "webcc/http_response_parser.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
class HttpRequest;
|
||||
class HttpResponse;
|
||||
|
||||
class HttpClient {
|
||||
public:
|
||||
HttpClient();
|
||||
|
||||
~HttpClient() = default;
|
||||
|
||||
DELETE_COPY_AND_ASSIGN(HttpClient);
|
||||
|
||||
void set_timeout_seconds(int timeout_seconds) {
|
||||
timeout_seconds_ = timeout_seconds;
|
||||
}
|
||||
|
||||
// Connect to the server, send the request, wait until the response is
|
||||
// received.
|
||||
Error Request(const HttpRequest& request, HttpResponse* response);
|
||||
HttpResponsePtr response() const { return response_; }
|
||||
|
||||
Error error() const { return error_; }
|
||||
|
||||
// Connect to server, send request, wait until response is received.
|
||||
bool Request(const HttpRequest& request);
|
||||
|
||||
private:
|
||||
Error Connect(const HttpRequest& request);
|
||||
|
||||
Error SendReqeust(const HttpRequest& request);
|
||||
|
||||
Error ReadResponse(HttpResponse* response);
|
||||
Error ReadResponse();
|
||||
|
||||
void CheckDeadline();
|
||||
|
||||
@@ -44,7 +49,10 @@ class HttpClient {
|
||||
|
||||
std::array<char, kBufferSize> buffer_;
|
||||
|
||||
std::unique_ptr<HttpResponseParser> parser_;
|
||||
HttpResponsePtr response_;
|
||||
std::unique_ptr<HttpResponseParser> response_parser_;
|
||||
|
||||
Error error_ = kNoError;
|
||||
|
||||
// Maximum seconds to wait before the client cancels the operation.
|
||||
// Only for receiving response from server.
|
||||
@@ -52,8 +60,6 @@ class HttpClient {
|
||||
|
||||
// Timer for the timeout control.
|
||||
boost::asio::deadline_timer deadline_timer_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(HttpClient);
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#include "webcc/http_message.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "boost/algorithm/string.hpp"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
void HttpMessage::SetHeader(const std::string& name, const std::string& value) {
|
||||
@@ -13,4 +17,45 @@ void HttpMessage::SetHeader(const std::string& name, const std::string& value) {
|
||||
headers_.push_back({ name, value });
|
||||
}
|
||||
|
||||
void HttpMessage::Dump(std::ostream& os, std::size_t indent,
|
||||
const std::string& prefix) const {
|
||||
std::string indent_str;
|
||||
if (indent > 0) {
|
||||
indent_str.append(indent, ' ');
|
||||
}
|
||||
indent_str.append(prefix);
|
||||
|
||||
os << indent_str << start_line_;
|
||||
|
||||
for (const HttpHeader& h : headers_) {
|
||||
os << indent_str << h.name << ": " << h.value << std::endl;
|
||||
}
|
||||
|
||||
os << std::endl;
|
||||
|
||||
if (!content_.empty()) {
|
||||
if (indent == 0) {
|
||||
os << content_ << std::endl;
|
||||
} else {
|
||||
std::vector<std::string> splitted;
|
||||
boost::split(splitted, content_, boost::is_any_of("\r\n"));
|
||||
for (const std::string& line : splitted) {
|
||||
os << indent_str << line << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string HttpMessage::Dump(std::size_t indent,
|
||||
const std::string& prefix) const {
|
||||
std::stringstream ss;
|
||||
Dump(ss, indent, prefix);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const HttpMessage& message) {
|
||||
message.Dump(os);
|
||||
return os;
|
||||
}
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
@@ -53,6 +53,14 @@ class HttpMessage {
|
||||
SetContentLength(content_.size());
|
||||
}
|
||||
|
||||
// Dump to output stream.
|
||||
void Dump(std::ostream& os, std::size_t indent = 0,
|
||||
const std::string& prefix = "") const;
|
||||
|
||||
// Dump to string, only used by logger.
|
||||
std::string Dump(std::size_t indent = 0,
|
||||
const std::string& prefix = "") const;
|
||||
|
||||
protected:
|
||||
void SetContentLength(std::size_t content_length) {
|
||||
content_length_ = content_length;
|
||||
@@ -69,6 +77,8 @@ class HttpMessage {
|
||||
std::string content_;
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const HttpMessage& message);
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
#endif // WEBCC_HTTP_MESSAGE_H_
|
||||
|
||||
@@ -117,7 +117,8 @@ void HttpParser::ParseContentLength(const std::string& line) {
|
||||
}
|
||||
|
||||
void HttpParser::Finish() {
|
||||
message_->SetContent(content_);
|
||||
// Move temp content to message.
|
||||
message_->SetContent(std::move(content_));
|
||||
finished_ = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@ class HttpParser {
|
||||
|
||||
virtual ~HttpParser() = default;
|
||||
|
||||
DELETE_COPY_AND_ASSIGN(HttpParser);
|
||||
|
||||
bool finished() const {
|
||||
return finished_;
|
||||
}
|
||||
@@ -50,8 +52,6 @@ class HttpParser {
|
||||
bool content_length_parsed_;
|
||||
bool header_parsed_;
|
||||
bool finished_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(HttpParser);
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
@@ -1,27 +1,7 @@
|
||||
#include "webcc/http_request.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "boost/algorithm/string.hpp"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const HttpRequest& request) {
|
||||
os << request.start_line();
|
||||
|
||||
for (const HttpHeader& h : request.headers_) {
|
||||
os << h.name << ": " << h.value << std::endl;
|
||||
}
|
||||
|
||||
os << std::endl;
|
||||
|
||||
if (!request.content().empty()) {
|
||||
os << request.content() << std::endl;
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
void HttpRequest::SetHost(const std::string& host, const std::string& port) {
|
||||
host_ = host;
|
||||
port_ = port;
|
||||
@@ -73,10 +53,4 @@ std::vector<boost::asio::const_buffer> HttpRequest::ToBuffers() const {
|
||||
return buffers;
|
||||
}
|
||||
|
||||
std::string HttpRequest::Dump() const {
|
||||
std::stringstream ss;
|
||||
ss << *this;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef WEBCC_HTTP_REQUEST_H_
|
||||
#define WEBCC_HTTP_REQUEST_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -10,10 +11,6 @@
|
||||
|
||||
namespace webcc {
|
||||
|
||||
class HttpRequest;
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const HttpRequest& request);
|
||||
|
||||
class HttpRequest : public HttpMessage {
|
||||
public:
|
||||
HttpRequest() = default;
|
||||
@@ -60,12 +57,7 @@ class HttpRequest : public HttpMessage {
|
||||
// and not be changed until the write operation has completed.
|
||||
std::vector<boost::asio::const_buffer> ToBuffers() const;
|
||||
|
||||
// Dump as string, only used by logger.
|
||||
std::string Dump() const;
|
||||
|
||||
private:
|
||||
friend std::ostream& operator<<(std::ostream& os, const HttpRequest& request);
|
||||
|
||||
// HTTP method.
|
||||
std::string method_;
|
||||
|
||||
@@ -78,6 +70,8 @@ class HttpRequest : public HttpMessage {
|
||||
std::string port_;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<HttpRequest> HttpRequestPtr;
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
#endif // WEBCC_HTTP_REQUEST_H_
|
||||
|
||||
@@ -21,6 +21,8 @@ class HttpRequestHandler {
|
||||
HttpRequestHandler() = default;
|
||||
virtual ~HttpRequestHandler() = default;
|
||||
|
||||
DELETE_COPY_AND_ASSIGN(HttpRequestHandler);
|
||||
|
||||
// Put the session into the queue.
|
||||
void Enqueue(HttpSessionPtr session);
|
||||
|
||||
@@ -38,8 +40,6 @@ class HttpRequestHandler {
|
||||
|
||||
Queue<HttpSessionPtr> queue_;
|
||||
boost::thread_group workers_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(HttpRequestHandler);
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
@@ -1,26 +1,7 @@
|
||||
#include "webcc/http_response.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace webcc {
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const HttpResponse& response) {
|
||||
os << response.start_line();
|
||||
|
||||
for (const HttpHeader& h : response.headers_) {
|
||||
os << h.name << ": " << h.value << std::endl;
|
||||
}
|
||||
|
||||
os << std::endl;
|
||||
|
||||
if (!response.content().empty()) {
|
||||
os << response.content() << std::endl;
|
||||
}
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
namespace status_strings {
|
||||
|
||||
const std::string OK = "HTTP/1.1 200 OK\r\n";
|
||||
@@ -115,10 +96,4 @@ HttpResponse HttpResponse::Fault(HttpStatus::Enum status) {
|
||||
return response;
|
||||
}
|
||||
|
||||
std::string HttpResponse::Dump() const {
|
||||
std::stringstream ss;
|
||||
ss << *this;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef WEBCC_HTTP_RESPONSE_H_
|
||||
#define WEBCC_HTTP_RESPONSE_H_
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -10,10 +11,6 @@
|
||||
|
||||
namespace webcc {
|
||||
|
||||
class HttpResponse;
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const HttpResponse& response);
|
||||
|
||||
class HttpResponse : public HttpMessage {
|
||||
public:
|
||||
HttpResponse() = default;
|
||||
@@ -32,19 +29,15 @@ class HttpResponse : public HttpMessage {
|
||||
// and not be changed until the write operation has completed.
|
||||
std::vector<boost::asio::const_buffer> ToBuffers() const;
|
||||
|
||||
// Dump as string, only used by logger.
|
||||
std::string Dump() const;
|
||||
|
||||
// Get a fault response when HTTP status is not OK.
|
||||
static HttpResponse Fault(HttpStatus::Enum status);
|
||||
|
||||
private:
|
||||
friend std::ostream& operator<<(std::ostream& os,
|
||||
const HttpResponse& response);
|
||||
|
||||
int status_ = HttpStatus::kOK;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<HttpResponse> HttpResponsePtr;
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
#endif // WEBCC_HTTP_RESPONSE_H_
|
||||
|
||||
@@ -25,6 +25,8 @@ class HttpServer {
|
||||
|
||||
virtual ~HttpServer() = default;
|
||||
|
||||
DELETE_COPY_AND_ASSIGN(HttpServer);
|
||||
|
||||
// Run the server's io_service loop.
|
||||
void Run();
|
||||
|
||||
@@ -49,8 +51,6 @@ class HttpServer {
|
||||
|
||||
// Acceptor used to listen for incoming connections.
|
||||
boost::scoped_ptr<boost::asio::ip::tcp::acceptor> acceptor_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(HttpServer);
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
@@ -18,14 +18,13 @@ class HttpRequestHandler;
|
||||
|
||||
class HttpSession : public std::enable_shared_from_this<HttpSession> {
|
||||
public:
|
||||
HttpSession(const HttpSession&) = delete;
|
||||
HttpSession& operator=(const HttpSession&) = delete;
|
||||
|
||||
HttpSession(boost::asio::ip::tcp::socket socket,
|
||||
HttpRequestHandler* handler);
|
||||
|
||||
~HttpSession() = default;
|
||||
|
||||
DELETE_COPY_AND_ASSIGN(HttpSession);
|
||||
|
||||
const HttpRequest& request() const {
|
||||
return request_;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
#include "webcc/rest_async_client.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
void RestAsyncClient::Request(const std::string& method,
|
||||
const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponseHandler response_handler) {
|
||||
response_handler_ = response_handler;
|
||||
|
||||
HttpRequestPtr request(new webcc::HttpRequest());
|
||||
|
||||
request->set_method(method);
|
||||
request->set_url(url);
|
||||
request->SetHost(host_, port_);
|
||||
|
||||
if (!content.empty()) {
|
||||
request->SetContent(content);
|
||||
}
|
||||
|
||||
request->Build();
|
||||
|
||||
HttpAsyncClientPtr http_client(new HttpAsyncClient(io_context_));
|
||||
http_client->Request(request, response_handler_);
|
||||
}
|
||||
|
||||
} // namespace webcc
|
||||
@@ -0,0 +1,60 @@
|
||||
#ifndef WEBCC_REST_ASYNC_CLIENT_H_
|
||||
#define WEBCC_REST_ASYNC_CLIENT_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "webcc/globals.h"
|
||||
#include "webcc/http_async_client.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
class RestAsyncClient {
|
||||
public:
|
||||
RestAsyncClient(boost::asio::io_context& io_context,
|
||||
const std::string& host, const std::string& port)
|
||||
: io_context_(io_context), host_(host), port_(port) {
|
||||
}
|
||||
|
||||
void Get(const std::string& url,
|
||||
HttpResponseHandler response_handler) {
|
||||
Request(kHttpGet, url, "", response_handler);
|
||||
}
|
||||
|
||||
void Post(const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponseHandler response_handler) {
|
||||
Request(kHttpPost, url, content, response_handler);
|
||||
}
|
||||
|
||||
void Put(const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponseHandler response_handler) {
|
||||
Request(kHttpPut, url, content, response_handler);
|
||||
}
|
||||
|
||||
void Patch(const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponseHandler response_handler) {
|
||||
Request(kHttpPatch, url, content, response_handler);
|
||||
}
|
||||
|
||||
void Delete(const std::string& url,
|
||||
HttpResponseHandler response_handler) {
|
||||
Request(kHttpDelete, url, "", response_handler);
|
||||
}
|
||||
|
||||
private:
|
||||
void Request(const std::string& method,
|
||||
const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponseHandler response_handler);
|
||||
|
||||
boost::asio::io_context& io_context_;
|
||||
std::string host_;
|
||||
std::string port_;
|
||||
HttpResponseHandler response_handler_;
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
#endif // WEBCC_REST_ASYNC_CLIENT_H_
|
||||
@@ -8,8 +8,7 @@ namespace webcc {
|
||||
|
||||
bool RestClient::Request(const std::string& method,
|
||||
const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponse* response) {
|
||||
const std::string& content) {
|
||||
HttpRequest request;
|
||||
|
||||
request.set_method(method);
|
||||
@@ -23,9 +22,17 @@ bool RestClient::Request(const std::string& method,
|
||||
request.Build();
|
||||
|
||||
HttpClient http_client;
|
||||
Error error = http_client.Request(request, response);
|
||||
http_client.set_timeout_seconds(timeout_seconds_);
|
||||
|
||||
return error == kNoError;
|
||||
error_ = kNoError;
|
||||
|
||||
if (!http_client.Request(request)) {
|
||||
error_ = http_client.error();
|
||||
return false;
|
||||
}
|
||||
|
||||
response_ = http_client.response();
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
+29
-20
@@ -4,51 +4,60 @@
|
||||
#include <string>
|
||||
|
||||
#include "webcc/globals.h"
|
||||
#include "webcc/http_response.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
class HttpResponse;
|
||||
|
||||
class RestClient {
|
||||
public:
|
||||
RestClient(const std::string& host, const std::string& port)
|
||||
: host_(host), port_(port) {
|
||||
}
|
||||
|
||||
bool Get(const std::string& url, HttpResponse* response) {
|
||||
return Request(kHttpGet, url, "", response);
|
||||
void set_timeout_seconds(int timeout_seconds) {
|
||||
timeout_seconds_ = timeout_seconds;
|
||||
}
|
||||
|
||||
bool Post(const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponse* response) {
|
||||
return Request(kHttpPost, url, content, response);
|
||||
HttpResponsePtr response() const { return response_; }
|
||||
int response_status() const { return response_->status(); }
|
||||
const std::string& response_content() const { return response_->content(); }
|
||||
|
||||
Error error() const { return error_; }
|
||||
|
||||
bool Get(const std::string& url) {
|
||||
return Request(kHttpGet, url, "");
|
||||
}
|
||||
|
||||
bool Put(const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponse* response) {
|
||||
return Request(kHttpPut, url, content, response);
|
||||
bool Post(const std::string& url, const std::string& content) {
|
||||
return Request(kHttpPost, url, content);
|
||||
}
|
||||
|
||||
bool Patch(const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponse* response) {
|
||||
return Request(kHttpPatch, url, content, response);
|
||||
bool Put(const std::string& url, const std::string& content) {
|
||||
return Request(kHttpPut, url, content);
|
||||
}
|
||||
|
||||
bool Delete(const std::string& url, HttpResponse* response) {
|
||||
return Request(kHttpDelete, url, "", response);
|
||||
bool Patch(const std::string& url, const std::string& content) {
|
||||
return Request(kHttpPatch, url, content);
|
||||
}
|
||||
|
||||
bool Delete(const std::string& url) {
|
||||
return Request(kHttpDelete, url, "");
|
||||
}
|
||||
|
||||
private:
|
||||
bool Request(const std::string& method,
|
||||
const std::string& url,
|
||||
const std::string& content,
|
||||
HttpResponse* response);
|
||||
const std::string& content);
|
||||
|
||||
std::string host_;
|
||||
std::string port_;
|
||||
|
||||
// -1 means default timeout (normally 30s) will be used.
|
||||
int timeout_seconds_ = -1;
|
||||
|
||||
HttpResponsePtr response_;
|
||||
|
||||
Error error_ = kNoError;
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
@@ -12,6 +12,7 @@ namespace webcc {
|
||||
|
||||
class RestRequestHandler : public HttpRequestHandler {
|
||||
public:
|
||||
RestRequestHandler() = default;
|
||||
~RestRequestHandler() override = default;
|
||||
|
||||
bool Bind(RestServicePtr service, const std::string& url, bool is_regex);
|
||||
|
||||
@@ -14,6 +14,8 @@ class RestServiceManager {
|
||||
public:
|
||||
RestServiceManager() = default;
|
||||
|
||||
DELETE_COPY_AND_ASSIGN(RestServiceManager);
|
||||
|
||||
// Add a service and bind it with the given URL.
|
||||
// The |url| should start with "/" and will be treated as a regular expression
|
||||
// if |regex| is true.
|
||||
@@ -59,8 +61,6 @@ class RestServiceManager {
|
||||
};
|
||||
|
||||
std::vector<ServiceItem> service_items_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(RestServiceManager);
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
@@ -54,16 +54,14 @@ Error SoapClient::Call(const std::string& operation,
|
||||
http_client.set_timeout_seconds(timeout_seconds_);
|
||||
}
|
||||
|
||||
Error error = http_client.Request(http_request, &http_response);
|
||||
|
||||
if (error != kNoError) {
|
||||
return error;
|
||||
if (!http_client.Request(http_request)) {
|
||||
return http_client.error();
|
||||
}
|
||||
|
||||
SoapResponse soap_response;
|
||||
soap_response.set_result_name(result_name_);
|
||||
|
||||
if (!soap_response.FromXml(http_response.content())) {
|
||||
if (!soap_response.FromXml(http_client.response()->content())) {
|
||||
return kXmlError;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user