Rename RegisterService to Bind and add is_regex parameter; indent public/protected/private by one space; refine rest example.

master
Adam Gu 7 years ago
parent 1bab2da8f4
commit cdc61b4bf2

@ -26,18 +26,23 @@ public:
}
bool ListBooks() {
std::cout << "ListBooks" << std::endl;
webcc::HttpResponse http_response;
if (!rest_client_.Get("/books", &http_response)) {
return false;
}
std::cout << "result:\n" << http_response.content() << std::endl;
std::cout << http_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::endl;
Json::Value json(Json::objectValue);
json["id"] = id;
json["title"] = title;
@ -66,6 +71,8 @@ public:
}
bool GetBook(const std::string& id) {
std::cout << "GetBook: " << id << std::endl;
webcc::HttpResponse http_response;
if (!rest_client_.Get("/book/" + id, &http_response)) {
return false;
@ -78,13 +85,16 @@ public:
bool UpdateBook(const std::string& id,
const std::string& title,
double price) {
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;
webcc::HttpResponse http_response;
if (!rest_client_.Post("/book/" + id, JsonToString(json), &http_response)) {
if (!rest_client_.Put("/book/" + id, JsonToString(json), &http_response)) {
return false;
}
@ -93,6 +103,8 @@ public:
}
bool DeleteBook(const std::string& id) {
std::cout << "DeleteBook: " << id << std::endl;
webcc::HttpResponse http_response;
if (!rest_client_.Delete("/book/" + id, &http_response)) {
return false;
@ -108,119 +120,35 @@ private:
////////////////////////////////////////////////////////////////////////////////
std::string g_host;
std::string g_port;
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;
}
std::string GetUserInput() {
char input[256];
std::cout << ">> ";
std::cin.getline(input, 256);
return input;
}
bool ParseJsonInput(const std::string& input, Json::Value* root) {
Json::CharReaderBuilder builder;
std::stringstream stream(input);
std::string errs;
if (Json::parseFromStream(builder, stream, root, &errs)) {
return true;
} else {
std::cerr << errs << std::endl;
return false;
}
}
int main(int argc, char* argv[]) {
if (argc != 3) {
Help(argv[0]);
return 1;
}
LOG_INIT(webcc::VERB, 0);
g_host = argv[1];
g_port = argv[2];
// Type commands to execute actions.
// Commands: list, create, detail, update, delete and exit.
// Examples:
// >> list
// >> create 1 { "title": "1984", "price": 12.3 }
// >> detail 1
// >> update 1 { "title": "1Q84", "price": 32.1 }
// >> delete 1
// >> exit
// A very naive implementation of interaction mode.
while (true) {
std::string input = GetUserInput();
boost::trim(input);
std::string command;
std::size_t i = input.find(' ');
if (i == std::string::npos) {
command = input;
} else {
command = input.substr(0, i);
}
if (command == "exit") {
break;
}
LOG_INIT(webcc::ERRO, 0);
if (command == "list") {
BookListClient client(g_host, g_port);
client.ListBooks();
continue;
}
std::string host = argv[1];
std::string port = argv[2];
++i;
BookListClient list_client(host, port);
BookDetailClient detail_client(host, port);
std::size_t j = input.find(' ', i);
std::string id = input.substr(i, j - i);
i = j + 1;
if (command == "create") {
std::string json = input.substr(i);
Json::Value root;
if (ParseJsonInput(json, &root)) {
BookListClient client(g_host, g_port);
client.CreateBook(id, root["title"].asString(), root["price"].asDouble());
}
continue;
}
list_client.ListBooks();
list_client.CreateBook("1", "1984", 12.3);
if (command == "update") {
std::string json = input.substr(i);
Json::Value root;
if (ParseJsonInput(json, &root)) {
BookDetailClient client(g_host, g_port);
client.UpdateBook(id, root["title"].asString(), root["price"].asDouble());
}
continue;
}
if (command == "detail") {
BookDetailClient client(g_host, g_port);
client.GetBook(id);
continue;
}
if (command == "delete") {
BookDetailClient client(g_host, g_port);
client.DeleteBook(id);
continue;
}
}
detail_client.GetBook("1");
detail_client.UpdateBook("1", "1Q84", 32.1);
detail_client.GetBook("1");
detail_client.DeleteBook("1");
list_client.ListBooks();
return 0;
}

@ -62,6 +62,7 @@ public:
if (it != books_.end()) {
it->title = book.title;
it->price = book.price;
return true;
}
return false;
}
@ -162,8 +163,8 @@ bool BookDetailService::Get(const std::vector<std::string>& url_sub_matches,
return false;
}
// Update a book partially.
bool BookDetailService::Patch(const std::vector<std::string>& url_sub_matches,
// Update a book.
bool BookDetailService::Put(const std::vector<std::string>& url_sub_matches,
const std::string& request_content,
std::string* response_content) {
if (url_sub_matches.size() != 1) {

@ -35,7 +35,7 @@ class BookDetailService : public webcc::RestDetailService {
const webcc::UrlQuery& query,
std::string* response_content) final;
bool Patch(const std::vector<std::string>& url_sub_matches,
bool Put(const std::vector<std::string>& url_sub_matches,
const std::string& request_content,
std::string* response_content) final;

@ -26,11 +26,9 @@ int main(int argc, char* argv[]) {
try {
webcc::RestServer server(port, workers);
server.RegisterService(std::make_shared<BookListService>(),
"/books");
server.Bind(std::make_shared<BookListService>(), "/books", false);
server.RegisterService(std::make_shared<BookDetailService>(),
"/book/(\\d+)");
server.Bind(std::make_shared<BookDetailService>(), "/book/(\\d+)", true);
server.Run();

@ -24,8 +24,7 @@ int main(int argc, char* argv[]) {
try {
webcc::SoapServer server(port, workers);
server.RegisterService(std::make_shared<CalcService>(),
"/calculator");
server.Bind(std::make_shared<CalcService>(), "/calculator");
server.Run();

@ -91,8 +91,8 @@ Parameter::Parameter(const std::string& key, bool value)
}
Parameter::Parameter(Parameter&& rhs)
: key_(std::move(rhs.key_))
, value_(std::move(rhs.value_)) {
: key_(std::move(rhs.key_)),
value_(std::move(rhs.value_)) {
}
Parameter& Parameter::operator=(Parameter&& rhs) {

@ -36,8 +36,7 @@ HttpClient::HttpClient()
CheckDeadline();
}
Error HttpClient::MakeRequest(const HttpRequest& request,
HttpResponse* response) {
Error HttpClient::Request(const HttpRequest& request, HttpResponse* response) {
assert(response != NULL);
Error error = kNoError;

@ -21,17 +21,13 @@ public:
HttpClient();
~HttpClient() = default;
HttpClient(const HttpClient&) = delete;
HttpClient& operator=(const HttpClient&) = delete;
void set_timeout_seconds(int timeout_seconds) {
timeout_seconds_ = timeout_seconds;
}
// Make a HTTP request.
// Connect to the server, send the request, wait until the response is
// received.
Error MakeRequest(const HttpRequest& request, HttpResponse* response);
Error Request(const HttpRequest& request, HttpResponse* response);
private:
Error Connect(const HttpRequest& request);
@ -42,7 +38,6 @@ private:
void CheckDeadline();
private:
boost::asio::io_context io_context_;
boost::asio::ip::tcp::socket socket_;
@ -57,6 +52,8 @@ private:
// Timer for the timeout control.
boost::asio::deadline_timer deadline_timer_;
DISALLOW_COPY_AND_ASSIGN(HttpClient);
};
} // namespace webcc

@ -51,13 +51,12 @@ public:
SetContentLength(content_.size());
}
private:
protected:
void SetContentLength(std::size_t content_length) {
content_length_ = content_length;
SetHeader(kContentLength, std::to_string(content_length));
}
protected:
// Start line with trailing "\r\n".
std::string start_line_;

@ -17,7 +17,6 @@ private:
// Parse HTTP start line; E.g., "HTTP/1.1 200 OK".
Error ParseStartLine(const std::string& line) override;
private:
// The result response message.
HttpResponse* response_;
};

@ -10,6 +10,7 @@
#include "boost/scoped_ptr.hpp"
#include "boost/thread/thread.hpp"
#include "webcc/globals.h"
#include "webcc/http_session.h"
namespace webcc {
@ -20,9 +21,6 @@ class HttpRequestHandler;
// NOTE: Only support IPv4.
class HttpServer {
public:
HttpServer(const HttpServer&) = delete;
HttpServer& operator=(const HttpServer&) = delete;
HttpServer(unsigned short port, std::size_t workers);
virtual ~HttpServer() = default;
@ -40,7 +38,6 @@ private:
// Get the handler for incoming requests.
virtual HttpRequestHandler* GetRequestHandler() = 0;
private:
// The number of worker threads.
std::size_t workers_;
@ -52,6 +49,8 @@ private:
// Acceptor used to listen for incoming connections.
boost::scoped_ptr<boost::asio::ip::tcp::acceptor> acceptor_;
DISALLOW_COPY_AND_ASSIGN(HttpServer);
};
} // namespace webcc

@ -48,7 +48,6 @@ private:
void AsyncWrite();
void WriteHandler(boost::system::error_code ec, std::size_t length);
private:
// Socket for the connection.
boost::asio::ip::tcp::socket socket_;

@ -23,7 +23,7 @@ bool RestClient::Request(const std::string& method,
request.Build();
HttpClient http_client;
Error error = http_client.MakeRequest(request, response);
Error error = http_client.Request(request, response);
return error == kNoError;
}

@ -5,9 +5,10 @@
namespace webcc {
bool RestRequestHandler::RegisterService(RestServicePtr service,
const std::string& url) {
return service_manager_.AddService(service, url);
bool RestRequestHandler::Bind(RestServicePtr service,
const std::string& url,
bool is_regex) {
return service_manager_.AddService(service, url, is_regex);
}
void RestRequestHandler::HandleSession(HttpSessionPtr session) {

@ -12,15 +12,11 @@ class RestRequestHandler : public HttpRequestHandler {
public:
~RestRequestHandler() override = default;
// Register a REST service to the given URL path.
// The URL should start with "/" and could be a regular expression or not.
// E.g., "/instances". "/instances/(\\d+)"
bool RegisterService(RestServicePtr service, const std::string& url);
bool Bind(RestServicePtr service, const std::string& url, bool is_regex);
private:
void HandleSession(HttpSessionPtr session) override;
private:
RestServiceManager service_manager_;
};

@ -17,13 +17,16 @@ public:
~RestServer() override = default;
// Register a REST service to the given URL path.
// The URL should start with "/" and could be a regular expression or not.
// E.g., "/instances". "/instances/(\\d+)"
// NOTE: Registering to the same URL multiple times is allowed, but only the
// last one takes effect.
bool RegisterService(RestServicePtr service, const std::string& url) {
return request_handler_.RegisterService(service, url);
// Bind a REST service to the given URL path.
// The URL should start with "/" and it will be treated as a regular
// expression if |is_regex| is true.
// Examples:
// - "/instances"
// - "/instances/(\\d+)"
// Binding to the same URL multiple times is allowed, but only the last one
// takes effect.
bool Bind(RestServicePtr service, const std::string& url, bool is_regex) {
return request_handler_.Bind(service, url, is_regex);
}
private:
@ -31,7 +34,6 @@ private:
return &request_handler_;
}
private:
RestRequestHandler request_handler_;
};

@ -7,10 +7,16 @@
namespace webcc {
bool RestServiceManager::AddService(RestServicePtr service,
const std::string& url) {
const std::string& url,
bool is_regex) {
assert(service);
ServiceItem item(service, url);
ServiceItem item(service, url, is_regex);
if (!is_regex) {
service_items_.push_back(std::move(item));
return true;
}
std::regex::flag_type flags = std::regex::ECMAScript | std::regex::icase;
@ -18,16 +24,14 @@ bool RestServiceManager::AddService(RestServicePtr service,
// Compile the regex.
item.url_regex.assign(url, flags);
service_items_.push_back(item);
service_items_.push_back(std::move(item));
return true;
} catch (std::regex_error& e) {
LOG_ERRO("URL is not a valid regular expression: %s", e.what());
}
return false;
}
}
RestServicePtr RestServiceManager::GetService(
const std::string& url,
@ -36,6 +40,7 @@ RestServicePtr RestServiceManager::GetService(
assert(sub_matches != NULL);
for (ServiceItem& item : service_items_) {
if (item.is_regex) {
std::smatch match;
if (std::regex_match(url, match, item.url_regex)) {
@ -47,6 +52,11 @@ RestServicePtr RestServiceManager::GetService(
return item.service;
}
} else {
if (item.url == url) {
return item.service;
}
}
}
return RestServicePtr();

@ -13,9 +13,11 @@ public:
RestServiceManager() = default;
// Add a service and bind it with the given URL.
// The |url| should start with "/" and could be a regular expression or not.
// E.g., "/instances". "/instances/(\\d+)"
bool AddService(RestServicePtr service, const std::string& url);
// The |url| should start with "/" and will be treated as a regular expression
// if |regex| is true.
// Examples: "/instances", "/instances/(\\d+)".
bool AddService(RestServicePtr service, const std::string& url,
bool is_regex);
// The |sub_matches| is only available when the |url| bound to the
// service is a regular expression and has sub-expressions.
@ -27,17 +29,19 @@ public:
private:
class ServiceItem {
public:
ServiceItem(RestServicePtr _service, const std::string& _url)
: service(_service), url(_url) {
ServiceItem(RestServicePtr _service, const std::string& _url,
bool _is_regex)
: service(_service), url(_url), is_regex(_is_regex) {
}
ServiceItem(const ServiceItem& rhs) = default;
ServiceItem& operator=(const ServiceItem& rhs) = default;
ServiceItem(const ServiceItem&) = default;
ServiceItem& operator=(const ServiceItem&) = default;
ServiceItem(ServiceItem&& rhs)
: url(std::move(rhs.url)),
url_regex(std::move(rhs.url_regex)),
service(rhs.service) { // No move
: service(rhs.service),
url(std::move(rhs.url)),
is_regex(rhs.is_regex),
url_regex(std::move(rhs.url_regex)) {
}
RestServicePtr service;
@ -45,6 +49,9 @@ private:
// URL string, e.g., "/instances/(\\d+)".
std::string url;
// If the URL is a regular expression or not.
bool is_regex;
// Compiled regex for URL string.
std::regex url_regex;
};

@ -53,7 +53,7 @@ Error SoapClient::Call(const std::string& operation,
http_client.set_timeout_seconds(timeout_seconds_);
}
Error error = http_client.MakeRequest(http_request, &http_response);
Error error = http_client.Request(http_request, &http_response);
if (error != kNoError) {
return error;

@ -25,7 +25,6 @@ protected:
std::vector<Parameter>&& parameters,
std::string* result);
protected:
// -1 means default timeout (normally 30s) will be used.
int timeout_seconds_ = -1;

@ -59,7 +59,6 @@ protected:
// Parse from SOAP body XML.
virtual bool FromXmlBody(pugi::xml_node xbody) = 0;
protected:
SoapNamespace soapenv_ns_; // SOAP envelope namespace.
SoapNamespace service_ns_; // Namespace for your web service.

@ -6,8 +6,7 @@
namespace webcc {
bool SoapRequestHandler::RegisterService(SoapServicePtr service,
const std::string& url) {
bool SoapRequestHandler::Bind(SoapServicePtr service, const std::string& url) {
assert(service);
url_service_map_[url] = service;

@ -12,11 +12,7 @@ public:
SoapRequestHandler() = default;
~SoapRequestHandler() override = default;
// Register a SOAP service to the given URL path.
// The |url| path must start with "/", e.g., "/calculator".
// Registering to the same URL multiple times is allowed, but only the last
// one takes effect.
bool RegisterService(SoapServicePtr service, const std::string& url);
bool Bind(SoapServicePtr service, const std::string& url);
private:
void HandleSession(HttpSessionPtr session) override;

@ -16,8 +16,12 @@ public:
~SoapServer() override = default;
bool RegisterService(SoapServicePtr service, const std::string& url) {
return request_handler_.RegisterService(service, url);
// Bind a SOAP service to the given URL path.
// The |url| path must start with "/", e.g., "/calculator".
// Binding to the same URL multiple times is allowed, but only the last
// one takes effect.
bool Bind(SoapServicePtr service, const std::string& url) {
return request_handler_.Bind(service, url);
}
private:

@ -53,7 +53,6 @@ private:
typedef Parameters::const_iterator ConstIterator;
ConstIterator Find(const std::string& key) const;
private:
Parameters parameters_;
};

@ -20,7 +20,7 @@ TEST(RestServiceManager, URL_RegexBasic) {
{
RestServicePtr service = std::make_shared<TestRestService>();
service_manager.AddService(service, "/instances/(\\d+)");
service_manager.AddService(service, "/instances/(\\d+)", true);
}
std::vector<std::string> sub_matches;
@ -39,3 +39,20 @@ TEST(RestServiceManager, URL_RegexBasic) {
EXPECT_FALSE(!!service);
}
TEST(RestServiceManager, URL_NonRegex) {
RestServiceManager service_manager;
{
RestServicePtr service = std::make_shared<TestRestService>();
service_manager.AddService(service, "/instances", false);
}
std::vector<std::string> sub_matches;
std::string url = "/instances";
RestServicePtr service = service_manager.GetService(url, &sub_matches);
EXPECT_TRUE(!!service);
EXPECT_EQ(0, sub_matches.size());
}

Loading…
Cancel
Save