Rename RegisterService to Bind and add is_regex parameter; indent public/protected/private by one space; refine rest example.
This commit is contained in:
@@ -26,18 +26,23 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool ListBooks() {
|
bool ListBooks() {
|
||||||
|
std::cout << "ListBooks" << std::endl;
|
||||||
|
|
||||||
webcc::HttpResponse http_response;
|
webcc::HttpResponse http_response;
|
||||||
if (!rest_client_.Get("/books", &http_response)) {
|
if (!rest_client_.Get("/books", &http_response)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "result:\n" << http_response.content() << std::endl;
|
std::cout << http_response.content() << std::endl;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CreateBook(const std::string& id,
|
bool CreateBook(const std::string& id,
|
||||||
const std::string& title,
|
const std::string& title,
|
||||||
double price) {
|
double price) {
|
||||||
|
std::cout << "CreateBook: " << id << " " << title << " " << price
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
Json::Value json(Json::objectValue);
|
Json::Value json(Json::objectValue);
|
||||||
json["id"] = id;
|
json["id"] = id;
|
||||||
json["title"] = title;
|
json["title"] = title;
|
||||||
@@ -66,6 +71,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool GetBook(const std::string& id) {
|
bool GetBook(const std::string& id) {
|
||||||
|
std::cout << "GetBook: " << id << std::endl;
|
||||||
|
|
||||||
webcc::HttpResponse http_response;
|
webcc::HttpResponse http_response;
|
||||||
if (!rest_client_.Get("/book/" + id, &http_response)) {
|
if (!rest_client_.Get("/book/" + id, &http_response)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -78,13 +85,16 @@ public:
|
|||||||
bool UpdateBook(const std::string& id,
|
bool UpdateBook(const std::string& id,
|
||||||
const std::string& title,
|
const std::string& title,
|
||||||
double price) {
|
double price) {
|
||||||
|
std::cout << "UpdateBook: " << id << " " << title << " " << price
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
// NOTE: ID is already in the URL.
|
// NOTE: ID is already in the URL.
|
||||||
Json::Value json(Json::objectValue);
|
Json::Value json(Json::objectValue);
|
||||||
json["title"] = title;
|
json["title"] = title;
|
||||||
json["price"] = price;
|
json["price"] = price;
|
||||||
|
|
||||||
webcc::HttpResponse http_response;
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +103,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool DeleteBook(const std::string& id) {
|
bool DeleteBook(const std::string& id) {
|
||||||
|
std::cout << "DeleteBook: " << id << std::endl;
|
||||||
|
|
||||||
webcc::HttpResponse http_response;
|
webcc::HttpResponse http_response;
|
||||||
if (!rest_client_.Delete("/book/" + id, &http_response)) {
|
if (!rest_client_.Delete("/book/" + id, &http_response)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -108,119 +120,35 @@ private:
|
|||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
std::string g_host;
|
|
||||||
std::string g_port;
|
|
||||||
|
|
||||||
void Help(const char* argv0) {
|
void Help(const char* argv0) {
|
||||||
std::cout << "Usage: " << argv0 << " <host> <port>" << std::endl;
|
std::cout << "Usage: " << argv0 << " <host> <port>" << std::endl;
|
||||||
std::cout << " E.g.," << std::endl;
|
std::cout << " E.g.," << std::endl;
|
||||||
std::cout << " " << argv0 << " localhost 8080" << 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[]) {
|
int main(int argc, char* argv[]) {
|
||||||
if (argc != 3) {
|
if (argc != 3) {
|
||||||
Help(argv[0]);
|
Help(argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INIT(webcc::VERB, 0);
|
LOG_INIT(webcc::ERRO, 0);
|
||||||
|
|
||||||
g_host = argv[1];
|
std::string host = argv[1];
|
||||||
g_port = argv[2];
|
std::string port = argv[2];
|
||||||
|
|
||||||
// Type commands to execute actions.
|
BookListClient list_client(host, port);
|
||||||
// Commands: list, create, detail, update, delete and exit.
|
BookDetailClient detail_client(host, port);
|
||||||
// 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.
|
list_client.ListBooks();
|
||||||
while (true) {
|
list_client.CreateBook("1", "1984", 12.3);
|
||||||
std::string input = GetUserInput();
|
|
||||||
boost::trim(input);
|
|
||||||
|
|
||||||
std::string command;
|
detail_client.GetBook("1");
|
||||||
std::size_t i = input.find(' ');
|
detail_client.UpdateBook("1", "1Q84", 32.1);
|
||||||
if (i == std::string::npos) {
|
detail_client.GetBook("1");
|
||||||
command = input;
|
detail_client.DeleteBook("1");
|
||||||
} else {
|
|
||||||
command = input.substr(0, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (command == "exit") {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (command == "list") {
|
|
||||||
BookListClient client(g_host, g_port);
|
|
||||||
client.ListBooks();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
++i;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
list_client.ListBooks();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ public:
|
|||||||
if (it != books_.end()) {
|
if (it != books_.end()) {
|
||||||
it->title = book.title;
|
it->title = book.title;
|
||||||
it->price = book.price;
|
it->price = book.price;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -162,8 +163,8 @@ bool BookDetailService::Get(const std::vector<std::string>& url_sub_matches,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update a book partially.
|
// Update a book.
|
||||||
bool BookDetailService::Patch(const std::vector<std::string>& url_sub_matches,
|
bool BookDetailService::Put(const std::vector<std::string>& url_sub_matches,
|
||||||
const std::string& request_content,
|
const std::string& request_content,
|
||||||
std::string* response_content) {
|
std::string* response_content) {
|
||||||
if (url_sub_matches.size() != 1) {
|
if (url_sub_matches.size() != 1) {
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class BookDetailService : public webcc::RestDetailService {
|
|||||||
const webcc::UrlQuery& query,
|
const webcc::UrlQuery& query,
|
||||||
std::string* response_content) final;
|
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,
|
const std::string& request_content,
|
||||||
std::string* response_content) final;
|
std::string* response_content) final;
|
||||||
|
|
||||||
|
|||||||
@@ -26,11 +26,9 @@ int main(int argc, char* argv[]) {
|
|||||||
try {
|
try {
|
||||||
webcc::RestServer server(port, workers);
|
webcc::RestServer server(port, workers);
|
||||||
|
|
||||||
server.RegisterService(std::make_shared<BookListService>(),
|
server.Bind(std::make_shared<BookListService>(), "/books", false);
|
||||||
"/books");
|
|
||||||
|
|
||||||
server.RegisterService(std::make_shared<BookDetailService>(),
|
server.Bind(std::make_shared<BookDetailService>(), "/book/(\\d+)", true);
|
||||||
"/book/(\\d+)");
|
|
||||||
|
|
||||||
server.Run();
|
server.Run();
|
||||||
|
|
||||||
|
|||||||
@@ -24,8 +24,7 @@ int main(int argc, char* argv[]) {
|
|||||||
try {
|
try {
|
||||||
webcc::SoapServer server(port, workers);
|
webcc::SoapServer server(port, workers);
|
||||||
|
|
||||||
server.RegisterService(std::make_shared<CalcService>(),
|
server.Bind(std::make_shared<CalcService>(), "/calculator");
|
||||||
"/calculator");
|
|
||||||
|
|
||||||
server.Run();
|
server.Run();
|
||||||
|
|
||||||
|
|||||||
@@ -91,8 +91,8 @@ Parameter::Parameter(const std::string& key, bool value)
|
|||||||
}
|
}
|
||||||
|
|
||||||
Parameter::Parameter(Parameter&& rhs)
|
Parameter::Parameter(Parameter&& rhs)
|
||||||
: key_(std::move(rhs.key_))
|
: key_(std::move(rhs.key_)),
|
||||||
, value_(std::move(rhs.value_)) {
|
value_(std::move(rhs.value_)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Parameter& Parameter::operator=(Parameter&& rhs) {
|
Parameter& Parameter::operator=(Parameter&& rhs) {
|
||||||
|
|||||||
@@ -36,8 +36,7 @@ HttpClient::HttpClient()
|
|||||||
CheckDeadline();
|
CheckDeadline();
|
||||||
}
|
}
|
||||||
|
|
||||||
Error HttpClient::MakeRequest(const HttpRequest& request,
|
Error HttpClient::Request(const HttpRequest& request, HttpResponse* response) {
|
||||||
HttpResponse* response) {
|
|
||||||
assert(response != NULL);
|
assert(response != NULL);
|
||||||
|
|
||||||
Error error = kNoError;
|
Error error = kNoError;
|
||||||
|
|||||||
@@ -21,17 +21,13 @@ public:
|
|||||||
HttpClient();
|
HttpClient();
|
||||||
~HttpClient() = default;
|
~HttpClient() = default;
|
||||||
|
|
||||||
HttpClient(const HttpClient&) = delete;
|
|
||||||
HttpClient& operator=(const HttpClient&) = delete;
|
|
||||||
|
|
||||||
void set_timeout_seconds(int timeout_seconds) {
|
void set_timeout_seconds(int timeout_seconds) {
|
||||||
timeout_seconds_ = timeout_seconds;
|
timeout_seconds_ = timeout_seconds;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make a HTTP request.
|
|
||||||
// Connect to the server, send the request, wait until the response is
|
// Connect to the server, send the request, wait until the response is
|
||||||
// received.
|
// received.
|
||||||
Error MakeRequest(const HttpRequest& request, HttpResponse* response);
|
Error Request(const HttpRequest& request, HttpResponse* response);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Error Connect(const HttpRequest& request);
|
Error Connect(const HttpRequest& request);
|
||||||
@@ -42,7 +38,6 @@ private:
|
|||||||
|
|
||||||
void CheckDeadline();
|
void CheckDeadline();
|
||||||
|
|
||||||
private:
|
|
||||||
boost::asio::io_context io_context_;
|
boost::asio::io_context io_context_;
|
||||||
|
|
||||||
boost::asio::ip::tcp::socket socket_;
|
boost::asio::ip::tcp::socket socket_;
|
||||||
@@ -57,6 +52,8 @@ private:
|
|||||||
|
|
||||||
// Timer for the timeout control.
|
// Timer for the timeout control.
|
||||||
boost::asio::deadline_timer deadline_timer_;
|
boost::asio::deadline_timer deadline_timer_;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(HttpClient);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace webcc
|
} // namespace webcc
|
||||||
|
|||||||
@@ -51,13 +51,12 @@ public:
|
|||||||
SetContentLength(content_.size());
|
SetContentLength(content_.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
void SetContentLength(std::size_t content_length) {
|
void SetContentLength(std::size_t content_length) {
|
||||||
content_length_ = content_length;
|
content_length_ = content_length;
|
||||||
SetHeader(kContentLength, std::to_string(content_length));
|
SetHeader(kContentLength, std::to_string(content_length));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
|
||||||
// Start line with trailing "\r\n".
|
// Start line with trailing "\r\n".
|
||||||
std::string start_line_;
|
std::string start_line_;
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ private:
|
|||||||
// Parse HTTP start line; E.g., "HTTP/1.1 200 OK".
|
// Parse HTTP start line; E.g., "HTTP/1.1 200 OK".
|
||||||
Error ParseStartLine(const std::string& line) override;
|
Error ParseStartLine(const std::string& line) override;
|
||||||
|
|
||||||
private:
|
|
||||||
// The result response message.
|
// The result response message.
|
||||||
HttpResponse* response_;
|
HttpResponse* response_;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include "boost/scoped_ptr.hpp"
|
#include "boost/scoped_ptr.hpp"
|
||||||
#include "boost/thread/thread.hpp"
|
#include "boost/thread/thread.hpp"
|
||||||
|
|
||||||
|
#include "webcc/globals.h"
|
||||||
#include "webcc/http_session.h"
|
#include "webcc/http_session.h"
|
||||||
|
|
||||||
namespace webcc {
|
namespace webcc {
|
||||||
@@ -20,9 +21,6 @@ class HttpRequestHandler;
|
|||||||
// NOTE: Only support IPv4.
|
// NOTE: Only support IPv4.
|
||||||
class HttpServer {
|
class HttpServer {
|
||||||
public:
|
public:
|
||||||
HttpServer(const HttpServer&) = delete;
|
|
||||||
HttpServer& operator=(const HttpServer&) = delete;
|
|
||||||
|
|
||||||
HttpServer(unsigned short port, std::size_t workers);
|
HttpServer(unsigned short port, std::size_t workers);
|
||||||
|
|
||||||
virtual ~HttpServer() = default;
|
virtual ~HttpServer() = default;
|
||||||
@@ -40,7 +38,6 @@ private:
|
|||||||
// Get the handler for incoming requests.
|
// Get the handler for incoming requests.
|
||||||
virtual HttpRequestHandler* GetRequestHandler() = 0;
|
virtual HttpRequestHandler* GetRequestHandler() = 0;
|
||||||
|
|
||||||
private:
|
|
||||||
// The number of worker threads.
|
// The number of worker threads.
|
||||||
std::size_t workers_;
|
std::size_t workers_;
|
||||||
|
|
||||||
@@ -52,6 +49,8 @@ private:
|
|||||||
|
|
||||||
// Acceptor used to listen for incoming connections.
|
// Acceptor used to listen for incoming connections.
|
||||||
boost::scoped_ptr<boost::asio::ip::tcp::acceptor> acceptor_;
|
boost::scoped_ptr<boost::asio::ip::tcp::acceptor> acceptor_;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(HttpServer);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace webcc
|
} // namespace webcc
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ private:
|
|||||||
void AsyncWrite();
|
void AsyncWrite();
|
||||||
void WriteHandler(boost::system::error_code ec, std::size_t length);
|
void WriteHandler(boost::system::error_code ec, std::size_t length);
|
||||||
|
|
||||||
private:
|
|
||||||
// Socket for the connection.
|
// Socket for the connection.
|
||||||
boost::asio::ip::tcp::socket socket_;
|
boost::asio::ip::tcp::socket socket_;
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ bool RestClient::Request(const std::string& method,
|
|||||||
request.Build();
|
request.Build();
|
||||||
|
|
||||||
HttpClient http_client;
|
HttpClient http_client;
|
||||||
Error error = http_client.MakeRequest(request, response);
|
Error error = http_client.Request(request, response);
|
||||||
|
|
||||||
return error == kNoError;
|
return error == kNoError;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,10 @@
|
|||||||
|
|
||||||
namespace webcc {
|
namespace webcc {
|
||||||
|
|
||||||
bool RestRequestHandler::RegisterService(RestServicePtr service,
|
bool RestRequestHandler::Bind(RestServicePtr service,
|
||||||
const std::string& url) {
|
const std::string& url,
|
||||||
return service_manager_.AddService(service, url);
|
bool is_regex) {
|
||||||
|
return service_manager_.AddService(service, url, is_regex);
|
||||||
}
|
}
|
||||||
|
|
||||||
void RestRequestHandler::HandleSession(HttpSessionPtr session) {
|
void RestRequestHandler::HandleSession(HttpSessionPtr session) {
|
||||||
|
|||||||
@@ -12,15 +12,11 @@ class RestRequestHandler : public HttpRequestHandler {
|
|||||||
public:
|
public:
|
||||||
~RestRequestHandler() override = default;
|
~RestRequestHandler() override = default;
|
||||||
|
|
||||||
// Register a REST service to the given URL path.
|
bool Bind(RestServicePtr service, const std::string& url, bool is_regex);
|
||||||
// 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);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void HandleSession(HttpSessionPtr session) override;
|
void HandleSession(HttpSessionPtr session) override;
|
||||||
|
|
||||||
private:
|
|
||||||
RestServiceManager service_manager_;
|
RestServiceManager service_manager_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+10
-8
@@ -17,13 +17,16 @@ public:
|
|||||||
|
|
||||||
~RestServer() override = default;
|
~RestServer() override = default;
|
||||||
|
|
||||||
// Register a REST service to the given URL path.
|
// Bind a REST service to the given URL path.
|
||||||
// The URL should start with "/" and could be a regular expression or not.
|
// The URL should start with "/" and it will be treated as a regular
|
||||||
// E.g., "/instances". "/instances/(\\d+)"
|
// expression if |is_regex| is true.
|
||||||
// NOTE: Registering to the same URL multiple times is allowed, but only the
|
// Examples:
|
||||||
// last one takes effect.
|
// - "/instances"
|
||||||
bool RegisterService(RestServicePtr service, const std::string& url) {
|
// - "/instances/(\\d+)"
|
||||||
return request_handler_.RegisterService(service, url);
|
// 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:
|
private:
|
||||||
@@ -31,7 +34,6 @@ private:
|
|||||||
return &request_handler_;
|
return &request_handler_;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
|
||||||
RestRequestHandler request_handler_;
|
RestRequestHandler request_handler_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -7,10 +7,16 @@
|
|||||||
namespace webcc {
|
namespace webcc {
|
||||||
|
|
||||||
bool RestServiceManager::AddService(RestServicePtr service,
|
bool RestServiceManager::AddService(RestServicePtr service,
|
||||||
const std::string& url) {
|
const std::string& url,
|
||||||
|
bool is_regex) {
|
||||||
assert(service);
|
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;
|
std::regex::flag_type flags = std::regex::ECMAScript | std::regex::icase;
|
||||||
|
|
||||||
@@ -18,16 +24,14 @@ bool RestServiceManager::AddService(RestServicePtr service,
|
|||||||
// Compile the regex.
|
// Compile the regex.
|
||||||
item.url_regex.assign(url, flags);
|
item.url_regex.assign(url, flags);
|
||||||
|
|
||||||
service_items_.push_back(item);
|
service_items_.push_back(std::move(item));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} catch (std::regex_error& e) {
|
} catch (std::regex_error& e) {
|
||||||
LOG_ERRO("URL is not a valid regular expression: %s", e.what());
|
LOG_ERRO("URL is not a valid regular expression: %s", e.what());
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
RestServicePtr RestServiceManager::GetService(
|
RestServicePtr RestServiceManager::GetService(
|
||||||
const std::string& url,
|
const std::string& url,
|
||||||
@@ -36,6 +40,7 @@ RestServicePtr RestServiceManager::GetService(
|
|||||||
assert(sub_matches != NULL);
|
assert(sub_matches != NULL);
|
||||||
|
|
||||||
for (ServiceItem& item : service_items_) {
|
for (ServiceItem& item : service_items_) {
|
||||||
|
if (item.is_regex) {
|
||||||
std::smatch match;
|
std::smatch match;
|
||||||
|
|
||||||
if (std::regex_match(url, match, item.url_regex)) {
|
if (std::regex_match(url, match, item.url_regex)) {
|
||||||
@@ -47,6 +52,11 @@ RestServicePtr RestServiceManager::GetService(
|
|||||||
|
|
||||||
return item.service;
|
return item.service;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (item.url == url) {
|
||||||
|
return item.service;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return RestServicePtr();
|
return RestServicePtr();
|
||||||
|
|||||||
@@ -13,9 +13,11 @@ public:
|
|||||||
RestServiceManager() = default;
|
RestServiceManager() = default;
|
||||||
|
|
||||||
// Add a service and bind it with the given URL.
|
// Add a service and bind it with the given URL.
|
||||||
// The |url| should start with "/" and could be a regular expression or not.
|
// The |url| should start with "/" and will be treated as a regular expression
|
||||||
// E.g., "/instances". "/instances/(\\d+)"
|
// if |regex| is true.
|
||||||
bool AddService(RestServicePtr service, const std::string& url);
|
// 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
|
// The |sub_matches| is only available when the |url| bound to the
|
||||||
// service is a regular expression and has sub-expressions.
|
// service is a regular expression and has sub-expressions.
|
||||||
@@ -27,17 +29,19 @@ public:
|
|||||||
private:
|
private:
|
||||||
class ServiceItem {
|
class ServiceItem {
|
||||||
public:
|
public:
|
||||||
ServiceItem(RestServicePtr _service, const std::string& _url)
|
ServiceItem(RestServicePtr _service, const std::string& _url,
|
||||||
: service(_service), url(_url) {
|
bool _is_regex)
|
||||||
|
: service(_service), url(_url), is_regex(_is_regex) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ServiceItem(const ServiceItem& rhs) = default;
|
ServiceItem(const ServiceItem&) = default;
|
||||||
ServiceItem& operator=(const ServiceItem& rhs) = default;
|
ServiceItem& operator=(const ServiceItem&) = default;
|
||||||
|
|
||||||
ServiceItem(ServiceItem&& rhs)
|
ServiceItem(ServiceItem&& rhs)
|
||||||
: url(std::move(rhs.url)),
|
: service(rhs.service),
|
||||||
url_regex(std::move(rhs.url_regex)),
|
url(std::move(rhs.url)),
|
||||||
service(rhs.service) { // No move
|
is_regex(rhs.is_regex),
|
||||||
|
url_regex(std::move(rhs.url_regex)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
RestServicePtr service;
|
RestServicePtr service;
|
||||||
@@ -45,6 +49,9 @@ private:
|
|||||||
// URL string, e.g., "/instances/(\\d+)".
|
// URL string, e.g., "/instances/(\\d+)".
|
||||||
std::string url;
|
std::string url;
|
||||||
|
|
||||||
|
// If the URL is a regular expression or not.
|
||||||
|
bool is_regex;
|
||||||
|
|
||||||
// Compiled regex for URL string.
|
// Compiled regex for URL string.
|
||||||
std::regex url_regex;
|
std::regex url_regex;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ Error SoapClient::Call(const std::string& operation,
|
|||||||
http_client.set_timeout_seconds(timeout_seconds_);
|
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) {
|
if (error != kNoError) {
|
||||||
return error;
|
return error;
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ protected:
|
|||||||
std::vector<Parameter>&& parameters,
|
std::vector<Parameter>&& parameters,
|
||||||
std::string* result);
|
std::string* result);
|
||||||
|
|
||||||
protected:
|
|
||||||
// -1 means default timeout (normally 30s) will be used.
|
// -1 means default timeout (normally 30s) will be used.
|
||||||
int timeout_seconds_ = -1;
|
int timeout_seconds_ = -1;
|
||||||
|
|
||||||
|
|||||||
@@ -59,7 +59,6 @@ protected:
|
|||||||
// Parse from SOAP body XML.
|
// Parse from SOAP body XML.
|
||||||
virtual bool FromXmlBody(pugi::xml_node xbody) = 0;
|
virtual bool FromXmlBody(pugi::xml_node xbody) = 0;
|
||||||
|
|
||||||
protected:
|
|
||||||
SoapNamespace soapenv_ns_; // SOAP envelope namespace.
|
SoapNamespace soapenv_ns_; // SOAP envelope namespace.
|
||||||
SoapNamespace service_ns_; // Namespace for your web service.
|
SoapNamespace service_ns_; // Namespace for your web service.
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,7 @@
|
|||||||
|
|
||||||
namespace webcc {
|
namespace webcc {
|
||||||
|
|
||||||
bool SoapRequestHandler::RegisterService(SoapServicePtr service,
|
bool SoapRequestHandler::Bind(SoapServicePtr service, const std::string& url) {
|
||||||
const std::string& url) {
|
|
||||||
assert(service);
|
assert(service);
|
||||||
|
|
||||||
url_service_map_[url] = service;
|
url_service_map_[url] = service;
|
||||||
|
|||||||
@@ -12,11 +12,7 @@ public:
|
|||||||
SoapRequestHandler() = default;
|
SoapRequestHandler() = default;
|
||||||
~SoapRequestHandler() override = default;
|
~SoapRequestHandler() override = default;
|
||||||
|
|
||||||
// Register a SOAP service to the given URL path.
|
bool Bind(SoapServicePtr service, const std::string& url);
|
||||||
// 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);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void HandleSession(HttpSessionPtr session) override;
|
void HandleSession(HttpSessionPtr session) override;
|
||||||
|
|||||||
@@ -16,8 +16,12 @@ public:
|
|||||||
|
|
||||||
~SoapServer() override = default;
|
~SoapServer() override = default;
|
||||||
|
|
||||||
bool RegisterService(SoapServicePtr service, const std::string& url) {
|
// Bind a SOAP service to the given URL path.
|
||||||
return request_handler_.RegisterService(service, url);
|
// 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:
|
private:
|
||||||
|
|||||||
@@ -53,7 +53,6 @@ private:
|
|||||||
typedef Parameters::const_iterator ConstIterator;
|
typedef Parameters::const_iterator ConstIterator;
|
||||||
ConstIterator Find(const std::string& key) const;
|
ConstIterator Find(const std::string& key) const;
|
||||||
|
|
||||||
private:
|
|
||||||
Parameters parameters_;
|
Parameters parameters_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ TEST(RestServiceManager, URL_RegexBasic) {
|
|||||||
{
|
{
|
||||||
RestServicePtr service = std::make_shared<TestRestService>();
|
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;
|
std::vector<std::string> sub_matches;
|
||||||
@@ -39,3 +39,20 @@ TEST(RestServiceManager, URL_RegexBasic) {
|
|||||||
|
|
||||||
EXPECT_FALSE(!!service);
|
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());
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user