Server API rework.

This commit is contained in:
Chunting Gu
2019-06-26 17:34:39 +08:00
parent a65294aeec
commit a3cab444dc
20 changed files with 341 additions and 384 deletions
+1 -2
View File
@@ -25,8 +25,7 @@ const char CRLF[] = { '\r', '\n' };
// -----------------------------------------------------------------------------
// Read entire file into string.
static bool ReadFile(const Path& path, std::string* output) {
bool ReadFile(const Path& path, std::string* output) {
// Flag "ate": seek to the end of stream immediately after open.
bfs::ifstream stream{ path, std::ios::binary | std::ios::ate };
if (stream.fail()) {
+3
View File
@@ -19,6 +19,9 @@ using Path = boost::filesystem::path;
using Payload = std::vector<boost::asio::const_buffer>;
// Read entire file into string.
bool ReadFile(const Path& path, std::string* output);
// -----------------------------------------------------------------------------
using Header = std::pair<std::string, std::string>;
+9
View File
@@ -5,6 +5,7 @@
#include <exception>
#include <iosfwd>
#include <string>
#include <vector>
#include "webcc/config.h"
@@ -43,6 +44,14 @@ namespace webcc {
// -----------------------------------------------------------------------------
using Strings = std::vector<std::string>;
// Regex sub-matches of the URL (usually resource ID's).
// Could also be considered as arguments, so named as UrlArgs.
using UrlArgs = std::vector<std::string>;
// -----------------------------------------------------------------------------
const char* const kCRLF = "\r\n";
const std::size_t kInvalidLength = std::string::npos;
+14 -19
View File
@@ -23,33 +23,24 @@ public:
~Request() override = default;
const std::string& method() const {
return method_;
}
const std::string& method() const { return method_; }
void set_method(const std::string& method) { method_ = method; }
void set_method(const std::string& method) {
method_ = method;
}
const Url& url() const { return url_; }
void set_url(const std::string& url) { url_.Init(url); }
const Url& url() const {
return url_;
}
const std::string& host() const { return url_.host(); }
const std::string& port() const { return url_.port(); }
void set_url(const std::string& url) {
url_.Init(url);
}
UrlQuery query() const { return UrlQuery(url_.query()); }
// TODO: Remove
void AddQuery(const std::string& key, const std::string& value) {
url_.AddQuery(key, value);
}
const std::string& host() const {
return url_.host();
}
const std::string& port() const {
return url_.port();
}
const UrlArgs& args() const { return args_; }
void set_args(const UrlArgs& args) { args_ = args; }
std::string port(const std::string& default_port) const {
return port().empty() ? default_port : port();
@@ -81,6 +72,10 @@ private:
Url url_;
// The URL regex matched arguments (usually resource ID's).
// Used by server only.
UrlArgs args_;
std::vector<FormPartPtr> form_parts_;
std::string boundary_;
+83 -33
View File
@@ -1,8 +1,11 @@
#include "webcc/request_handler.h"
#include <algorithm>
#include <fstream>
#include <utility> // for move()
#include <vector>
#include "boost/algorithm/string.hpp"
#include "boost/filesystem/fstream.hpp"
#include "webcc/logger.h"
#include "webcc/request.h"
@@ -13,15 +16,41 @@
#include "webcc/gzip.h"
#endif
namespace bfs = boost::filesystem;
namespace webcc {
RequestHandler::RequestHandler(const std::string& doc_root)
RequestHandler::RequestHandler(const Path& doc_root)
: doc_root_(doc_root) {
}
bool RequestHandler::Bind(ServicePtr service, const std::string& url,
bool is_regex) {
return service_manager_.Add(service, url, is_regex);
bool RequestHandler::Route(const std::string& url, ViewPtr view,
const Strings& methods) {
assert(view);
// TODO: More error check
routes_.push_back({ url, {}, view, methods });
return true;
}
bool RequestHandler::Route(const RegexUrl& regex_url, ViewPtr view,
const Strings& methods) {
assert(view);
// TODO: More error check
try {
routes_.push_back({ "", regex_url(), view, methods });
} catch (const std::regex_error& e) {
LOG_ERRO("Not a valid regular expression: %s", e.what());
return false;
}
return true;
}
void RequestHandler::Enqueue(ConnectionPtr connection) {
@@ -72,11 +101,11 @@ void RequestHandler::WorkerRoutine() {
break;
}
HandleConnection(connection);
Handle(connection);
}
}
void RequestHandler::HandleConnection(ConnectionPtr connection) {
void RequestHandler::Handle(ConnectionPtr connection) {
auto request = connection->request();
const Url& url = request->url();
@@ -84,11 +113,11 @@ void RequestHandler::HandleConnection(ConnectionPtr connection) {
LOG_INFO("Request URL path: %s", url.path().c_str());
// Get service by URL path.
auto service = service_manager_.Get(url.path(), &args);
// Find view
auto view = FindView(request->method(), url.path(), &args);
if (!service) {
LOG_WARN("No service matches the URL path: %s", url.path().c_str());
if (!view) {
LOG_WARN("No view matches the URL path: %s", url.path().c_str());
if (!ServeStatic(connection)) {
connection->SendResponse(Status::kNotFound);
@@ -97,9 +126,13 @@ void RequestHandler::HandleConnection(ConnectionPtr connection) {
return;
}
ResponsePtr response = service->Handle(request, args);
// Save the (regex matched) URL args to request object.
request->set_args(args);
// Ask the matched view to process the request.
ResponsePtr response = view->Handle(request);
// Send response back to client.
// Send the response back.
if (response) {
connection->SendResponse(response);
} else {
@@ -107,6 +140,38 @@ void RequestHandler::HandleConnection(ConnectionPtr connection) {
}
}
ViewPtr RequestHandler::FindView(const std::string& method,
const std::string& url, UrlArgs* args) {
assert(args != nullptr);
for (auto& route : routes_) {
if (std::find(route.methods.begin(), route.methods.end(), method) ==
route.methods.end()) {
continue;
}
if (route.url.empty()) {
std::smatch match;
if (std::regex_match(url, match, route.url_regex)) {
// Any sub-matches?
// Start from 1 because match[0] is the whole string itself.
for (size_t i = 1; i < match.size(); ++i) {
args->push_back(match[i].str());
}
return route.view;
}
} else {
if (boost::iequals(route.url, url)) {
return route.view;
}
}
}
return ViewPtr();
}
bool RequestHandler::ServeStatic(ConnectionPtr connection) {
auto request = connection->request();
std::string path = request->url().path();
@@ -116,33 +181,18 @@ bool RequestHandler::ServeStatic(ConnectionPtr connection) {
path += "index.html";
}
// Determine the file extension.
std::string extension;
std::size_t last_slash_pos = path.find_last_of("/");
std::size_t last_dot_pos = path.find_last_of(".");
if (last_dot_pos != std::string::npos && last_dot_pos > last_slash_pos) {
extension = path.substr(last_dot_pos + 1);
}
Path p = doc_root_ / path;
// Open the file to send back.
std::string full_path = doc_root_ + path;
std::ifstream ifs(full_path.c_str(), std::ios::in | std::ios::binary);
if (!ifs) {
// The file doesn't exist.
std::string content;
if (!ReadFile(p, &content)) {
connection->SendResponse(Status::kNotFound);
return false;
}
// Fill out the content to be sent to the client.
std::string content;
char buf[512];
while (ifs.read(buf, sizeof(buf)).gcount() > 0) {
content.append(buf, ifs.gcount());
}
auto response = std::make_shared<Response>(Status::kOK);
if (!content.empty()) {
std::string extension = p.extension().string();
response->SetContentType(media_types::FromExtension(extension), "");
response->SetContent(std::move(content), true);
}
+43 -7
View File
@@ -2,26 +2,50 @@
#define WEBCC_REQUEST_HANDLER_H_
#include <list>
#include <regex>
#include <thread>
#include <vector>
#include "webcc/connection.h"
#include "webcc/queue.h"
#include "webcc/service_manager.h"
#include "webcc/view.h"
namespace webcc {
// The common handler for all incoming requests.
// -----------------------------------------------------------------------------
// Wrapper for regular expression URL.
class RegexUrl {
public:
explicit RegexUrl(const std::string& url) : url_(url) {
}
std::regex operator()() const {
std::regex::flag_type flags = std::regex::ECMAScript | std::regex::icase;
return std::regex(url_, flags);
}
private:
std::string url_;
};
using R = RegexUrl; // A shortcut
// -----------------------------------------------------------------------------
class RequestHandler {
public:
explicit RequestHandler(const std::string& doc_root);
explicit RequestHandler(const Path& doc_root);
virtual ~RequestHandler() = default;
RequestHandler(const RequestHandler&) = delete;
RequestHandler& operator=(const RequestHandler&) = delete;
bool Bind(ServicePtr service, const std::string& url, bool is_regex);
bool Route(const std::string& url, ViewPtr view, const Strings& methods);
bool Route(const RegexUrl& regex_url, ViewPtr view, const Strings& methods);
// Put the connection into the queue.
void Enqueue(ConnectionPtr connection);
@@ -40,7 +64,11 @@ private:
// then send the response back to the client.
// The connection will keep alive if it's a persistent connection. When next
// request comes, this connection will be put back to the queue again.
virtual void HandleConnection(ConnectionPtr connection);
virtual void Handle(ConnectionPtr connection);
// Find the view by HTTP method and URL.
ViewPtr FindView(const std::string& method, const std::string& url,
UrlArgs* args);
// TODO
bool ServeStatic(ConnectionPtr connection);
@@ -48,15 +76,23 @@ private:
void SetContent(RequestPtr request, ResponsePtr response,
std::string&& content);
private:
struct RouteInfo {
std::string url;
std::regex url_regex;
ViewPtr view;
Strings methods;
};
private:
// The directory containing the files to be served.
std::string doc_root_;
Path doc_root_;
Queue<ConnectionPtr> queue_;
std::vector<std::thread> workers_;
ServiceManager service_manager_;
std::vector<RouteInfo> routes_;
};
} // namespace webcc
+1 -6
View File
@@ -11,8 +11,7 @@ using tcp = boost::asio::ip::tcp;
namespace webcc {
Server::Server(std::uint16_t port, std::size_t workers,
const std::string& doc_root)
Server::Server(std::uint16_t port, std::size_t workers, const Path& doc_root)
: acceptor_(io_context_), signals_(io_context_), workers_(workers),
request_handler_(doc_root) {
RegisterSignals();
@@ -52,10 +51,6 @@ Server::Server(std::uint16_t port, std::size_t workers,
}
}
bool Server::Bind(ServicePtr service, const std::string& url, bool is_regex) {
return request_handler_.Bind(service, url, is_regex);
}
void Server::Run() {
if (!acceptor_.is_open()) {
LOG_ERRO("Server is NOT going to run.");
+24 -18
View File
@@ -1,6 +1,7 @@
#ifndef WEBCC_SERVER_H_
#define WEBCC_SERVER_H_
#include <regex>
#include <string>
#include "boost/asio/io_context.hpp"
@@ -10,33 +11,41 @@
#include "webcc/connection.h"
#include "webcc/connection_pool.h"
#include "webcc/request_handler.h"
#include "webcc/service.h"
#include "webcc/view.h"
namespace webcc {
// HTTP server accepts TCP connections from TCP clients.
// NOTE: Only support IPv4.
class Server {
public:
Server(std::uint16_t port, std::size_t workers,
const std::string& doc_root = "");
Server(std::uint16_t port, std::size_t workers, const Path& doc_root = {});
virtual ~Server() = default;
Server(const Server&) = delete;
Server& operator=(const Server&) = delete;
// Bind a 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(ServicePtr service, const std::string& url, bool is_regex);
// Route a URL to a view.
// The URL should start with "/". E.g., "/instances".
bool Route(const std::string& url, ViewPtr view, const Strings& methods) {
return request_handler_.Route(url, view, methods);
}
// Run the server's io_service loop.
bool Route(const std::string& url, ViewPtr view) {
return Route(url, view, { methods::kGet });
}
// Route a regular expression URL to a view.
// The URL should start with "/" and be a regular expression.
// E.g., "/instances/(\\d+)".
bool Route(const RegexUrl& regex_url, ViewPtr view, const Strings& methods) {
return request_handler_.Route(regex_url, view, methods);
}
bool Route(const RegexUrl& regex_url, ViewPtr view) {
return request_handler_.Route(regex_url, view, { methods::kGet });
}
// Run the loop.
void Run();
private:
@@ -49,9 +58,6 @@ private:
// Wait for a request to stop the server.
void DoAwaitStop();
// Get the handler for incoming requests.
//virtual RequestHandler* GetRequestHandler();
// The io_context used to perform asynchronous operations.
boost::asio::io_context io_context_;
-67
View File
@@ -1,67 +0,0 @@
#include "webcc/service.h"
#include "webcc/logger.h"
namespace webcc {
// -----------------------------------------------------------------------------
ResponsePtr ListService::Handle(RequestPtr request, const UrlArgs& args) {
if (request->method() == methods::kGet) {
return Get(UrlQuery(request->url().query()));
}
if (request->method() == methods::kPost) {
return Post(request);
}
return ResponsePtr();
}
ResponsePtr ListService::Get(const UrlQuery& query) {
return ResponsePtr();
}
ResponsePtr ListService::Post(RequestPtr request) {
return ResponsePtr();
}
// -----------------------------------------------------------------------------
ResponsePtr DetailService::Handle(RequestPtr request, const UrlArgs& args) {
if (request->method() == methods::kGet) {
return Get(args, UrlQuery(request->url().query()));
}
if (request->method() == methods::kPut) {
return Put(request, args);
}
if (request->method() == methods::kPatch) {
return Patch(request, args);
}
if (request->method() == methods::kDelete) {
return Delete(args);
}
return ResponsePtr();
}
ResponsePtr DetailService::Get(const UrlArgs& args, const UrlQuery& query) {
return ResponsePtr();
}
ResponsePtr DetailService::Put(RequestPtr request, const UrlArgs& args) {
return ResponsePtr();
}
ResponsePtr DetailService::Patch(RequestPtr request, const UrlArgs& args) {
return ResponsePtr();
}
ResponsePtr DetailService::Delete(const UrlArgs& args) {
return ResponsePtr();
}
} // namespace webcc
-73
View File
@@ -1,73 +0,0 @@
#ifndef WEBCC_SERVICE_H_
#define WEBCC_SERVICE_H_
// NOTE:
// The design of RestListService and RestDetailService is very similar to
// XxxListView and XxxDetailView in Python Django Rest Framework.
// Deriving from them instead of RestService can simplify your own REST services
// a lot. But if you find the filtered parameters cannot meet your needs, feel
// free to derive from RestService directly.
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "webcc/globals.h"
#include "webcc/request.h"
#include "webcc/response.h"
#include "webcc/response_builder.h"
#include "webcc/url.h"
namespace webcc {
// -----------------------------------------------------------------------------
// Regex sub-matches of the URL (usually resource ID's).
// Could also be considered as arguments, so named as UrlArgs.
using UrlArgs = std::vector<std::string>;
// -----------------------------------------------------------------------------
// Base class for your service.
class Service {
public:
virtual ~Service() = default;
// Handle request, return response.
virtual ResponsePtr Handle(RequestPtr request, const UrlArgs& args) = 0;
};
using ServicePtr = std::shared_ptr<Service>;
// -----------------------------------------------------------------------------
class ListService : public Service {
public:
ResponsePtr Handle(RequestPtr request, const UrlArgs& args) override;
protected:
virtual ResponsePtr Get(const UrlQuery& query);
virtual ResponsePtr Post(RequestPtr request);
};
// -----------------------------------------------------------------------------
class DetailService : public Service {
public:
ResponsePtr Handle(RequestPtr request, const UrlArgs& args) override;
protected:
virtual ResponsePtr Get(const UrlArgs& args, const UrlQuery& query);
virtual ResponsePtr Put(RequestPtr request, const UrlArgs& args);
virtual ResponsePtr Patch(RequestPtr request, const UrlArgs& args);
virtual ResponsePtr Delete(const UrlArgs& args);
};
} // namespace webcc
#endif // WEBCC_SERVICE_H_
-59
View File
@@ -1,59 +0,0 @@
#include "webcc/service_manager.h"
#include <cassert>
#include "webcc/logger.h"
namespace webcc {
bool ServiceManager::Add(ServicePtr service, const std::string& url,
bool is_regex) {
assert(service);
Item item(service, url, is_regex);
if (!is_regex) {
items_.push_back(std::move(item));
return true;
}
std::regex::flag_type flags = std::regex::ECMAScript | std::regex::icase;
try {
// Compile the regex.
item.url_regex.assign(url, flags);
items_.push_back(std::move(item));
return true;
} catch (const std::regex_error& e) {
LOG_ERRO("URL is not a valid regular expression: %s", e.what());
return false;
}
}
ServicePtr ServiceManager::Get(const std::string& url, UrlArgs* args) {
assert(args != nullptr);
for (Item& item : items_) {
if (item.is_regex) {
std::smatch match;
if (std::regex_match(url, match, item.url_regex)) {
// Any sub-matches?
// NOTE: Start from 1 because match[0] is the whole string itself.
for (size_t i = 1; i < match.size(); ++i) {
args->push_back(match[i].str());
}
return item.service;
}
} else {
if (item.url == url) {
return item.service;
}
}
}
return ServicePtr();
}
} // namespace webcc
-66
View File
@@ -1,66 +0,0 @@
#ifndef WEBCC_SERVICE_MANAGER_H_
#define WEBCC_SERVICE_MANAGER_H_
#include <regex> // NOLINT
#include <string>
#include <utility> // for move()
#include <vector>
#include "webcc/service.h"
namespace webcc {
class ServiceManager {
public:
ServiceManager() = default;
ServiceManager(const ServiceManager&) = delete;
ServiceManager& operator=(const ServiceManager&) = delete;
// 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.
// Examples: "/instances", "/instances/(\\d+)".
bool Add(ServicePtr service, const std::string& url, bool is_regex);
// The |matches| is only available when the |url| bound to the service is a
// regular expression and has sub-expressions.
// E.g., the URL bound to the service is "/instances/(\\d+)", now match
// "/instances/12345" against it, you will get one match of "12345".
ServicePtr Get(const std::string& url, UrlArgs* args);
private:
class Item {
public:
Item(ServicePtr _service, const std::string& _url, bool _is_regex)
: service(_service), url(_url), is_regex(_is_regex) {
}
Item(const Item&) = default;
Item& operator=(const Item&) = default;
Item(Item&& rhs)
: service(rhs.service),
url(std::move(rhs.url)),
is_regex(rhs.is_regex),
url_regex(std::move(rhs.url_regex)) {
}
ServicePtr service;
// 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;
};
std::vector<Item> items_;
};
} // namespace webcc
#endif // WEBCC_SERVICE_MANAGER_H_
+43
View File
@@ -0,0 +1,43 @@
#include "webcc/view.h"
namespace webcc {
// -----------------------------------------------------------------------------
//
//ResponsePtr DetailView::Handle(RequestPtr request, const UrlArgs& args) {
// if (request->method() == methods::kGet) {
// return Get(args, UrlQuery(request->url().query()));
// }
//
// if (request->method() == methods::kPut) {
// return Put(request, args);
// }
//
// if (request->method() == methods::kPatch) {
// return Patch(request, args);
// }
//
// if (request->method() == methods::kDelete) {
// return Delete(args);
// }
//
// return ResponsePtr();
//}
//
//ResponsePtr DetailView::Get(const UrlArgs& args, const UrlQuery& query) {
// return ResponsePtr();
//}
//
//ResponsePtr DetailView::Put(RequestPtr request, const UrlArgs& args) {
// return ResponsePtr();
//}
//
//ResponsePtr DetailView::Patch(RequestPtr request, const UrlArgs& args) {
// return ResponsePtr();
//}
//
//ResponsePtr DetailView::Delete(const UrlArgs& args) {
// return ResponsePtr();
//}
} // namespace webcc
+22
View File
@@ -0,0 +1,22 @@
#ifndef WEBCC_VIEW_H_
#define WEBCC_VIEW_H_
#include <memory>
#include "webcc/request.h"
#include "webcc/response.h"
namespace webcc {
class View {
public:
virtual ~View() = default;
virtual ResponsePtr Handle(RequestPtr request) = 0;
};
using ViewPtr = std::shared_ptr<View>;
} // namespace webcc
#endif // WEBCC_VIEW_H_