Merge request handler to server; add FileBody for serving static files.
parent
91590b8ee8
commit
8b85248a34
@ -1,210 +0,0 @@
|
|||||||
#include "webcc/request_handler.h"
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <fstream>
|
|
||||||
#include <utility>
|
|
||||||
|
|
||||||
#include "boost/algorithm/string.hpp"
|
|
||||||
#include "boost/filesystem/fstream.hpp"
|
|
||||||
|
|
||||||
#include "webcc/logger.h"
|
|
||||||
#include "webcc/request.h"
|
|
||||||
#include "webcc/response.h"
|
|
||||||
#include "webcc/url.h"
|
|
||||||
#include "webcc/utility.h"
|
|
||||||
|
|
||||||
#if WEBCC_ENABLE_GZIP
|
|
||||||
#include "webcc/gzip.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace bfs = boost::filesystem;
|
|
||||||
|
|
||||||
namespace webcc {
|
|
||||||
|
|
||||||
RequestHandler::RequestHandler(const Path& doc_root) : doc_root_(doc_root) {
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
|
||||||
queue_.Push(connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
void RequestHandler::Start(std::size_t count) {
|
|
||||||
assert(count > 0 && workers_.size() == 0);
|
|
||||||
|
|
||||||
for (std::size_t i = 0; i < count; ++i) {
|
|
||||||
workers_.emplace_back(std::bind(&RequestHandler::WorkerRoutine, this));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void RequestHandler::Stop() {
|
|
||||||
LOG_INFO("Stopping workers...");
|
|
||||||
|
|
||||||
// Clear pending connections.
|
|
||||||
// The connections will be closed later (see Server::DoAwaitStop).
|
|
||||||
LOG_INFO("Clear pending connections...");
|
|
||||||
queue_.Clear();
|
|
||||||
|
|
||||||
// Enqueue a null connection to trigger the first worker to stop.
|
|
||||||
queue_.Push(ConnectionPtr());
|
|
||||||
|
|
||||||
for (auto& worker : workers_) {
|
|
||||||
if (worker.joinable()) {
|
|
||||||
worker.join();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_INFO("All workers have been stopped.");
|
|
||||||
}
|
|
||||||
|
|
||||||
void RequestHandler::WorkerRoutine() {
|
|
||||||
LOG_INFO("Worker is running.");
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
auto connection = queue_.PopOrWait();
|
|
||||||
|
|
||||||
if (!connection) {
|
|
||||||
LOG_INFO("Worker is going to stop.");
|
|
||||||
|
|
||||||
// For stopping next worker.
|
|
||||||
queue_.Push(ConnectionPtr());
|
|
||||||
|
|
||||||
// Stop the worker.
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
Handle(connection);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void RequestHandler::Handle(ConnectionPtr connection) {
|
|
||||||
auto request = connection->request();
|
|
||||||
|
|
||||||
const Url& url = request->url();
|
|
||||||
UrlArgs args;
|
|
||||||
|
|
||||||
LOG_INFO("Request URL path: %s", url.path().c_str());
|
|
||||||
|
|
||||||
// Find view
|
|
||||||
auto view = FindView(request->method(), url.path(), &args);
|
|
||||||
|
|
||||||
if (!view) {
|
|
||||||
LOG_WARN("No view matches the URL path: %s", url.path().c_str());
|
|
||||||
|
|
||||||
if (!ServeStatic(connection)) {
|
|
||||||
connection->SendResponse(Status::kNotFound);
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 the response back.
|
|
||||||
if (response) {
|
|
||||||
connection->SendResponse(response);
|
|
||||||
} else {
|
|
||||||
connection->SendResponse(Status::kNotImplemented);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
|
|
||||||
// If path ends in slash (i.e. is a directory) then add "index.html".
|
|
||||||
if (path[path.size() - 1] == '/') {
|
|
||||||
path += "index.html";
|
|
||||||
}
|
|
||||||
|
|
||||||
Path p = doc_root_ / path;
|
|
||||||
|
|
||||||
std::string data;
|
|
||||||
if (!utility::ReadFile(p, &data)) {
|
|
||||||
connection->SendResponse(Status::kNotFound);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto response = std::make_shared<Response>(Status::kOK);
|
|
||||||
|
|
||||||
if (!data.empty()) {
|
|
||||||
std::string extension = p.extension().string();
|
|
||||||
response->SetContentType(media_types::FromExtension(extension), "");
|
|
||||||
|
|
||||||
// TODO: Use FileBody instead for streaming.
|
|
||||||
// TODO: gzip
|
|
||||||
auto body = std::make_shared<StringBody>(std::move(data));
|
|
||||||
response->SetBody(body, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send response back to client.
|
|
||||||
connection->SendResponse(response);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace webcc
|
|
@ -1,97 +0,0 @@
|
|||||||
#ifndef WEBCC_REQUEST_HANDLER_H_
|
|
||||||
#define WEBCC_REQUEST_HANDLER_H_
|
|
||||||
|
|
||||||
#include <list>
|
|
||||||
#include <regex>
|
|
||||||
#include <thread>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "webcc/connection.h"
|
|
||||||
#include "webcc/queue.h"
|
|
||||||
#include "webcc/view.h"
|
|
||||||
|
|
||||||
namespace webcc {
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// 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 Path& doc_root);
|
|
||||||
|
|
||||||
virtual ~RequestHandler() = default;
|
|
||||||
|
|
||||||
RequestHandler(const RequestHandler&) = delete;
|
|
||||||
RequestHandler& operator=(const RequestHandler&) = delete;
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
// Start worker threads.
|
|
||||||
void Start(std::size_t count);
|
|
||||||
|
|
||||||
// Clear pending connections from the queue and stop worker threads.
|
|
||||||
void Stop();
|
|
||||||
|
|
||||||
private:
|
|
||||||
void WorkerRoutine();
|
|
||||||
|
|
||||||
// Handle a connection (or more precisely, the request inside it).
|
|
||||||
// Get the request from the connection, process it, prepare the response,
|
|
||||||
// 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 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);
|
|
||||||
|
|
||||||
private:
|
|
||||||
struct RouteInfo {
|
|
||||||
std::string url;
|
|
||||||
std::regex url_regex;
|
|
||||||
ViewPtr view;
|
|
||||||
Strings methods;
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
|
||||||
// The directory containing the files to be served.
|
|
||||||
Path doc_root_;
|
|
||||||
|
|
||||||
Queue<ConnectionPtr> queue_;
|
|
||||||
|
|
||||||
std::vector<std::thread> workers_;
|
|
||||||
|
|
||||||
std::vector<RouteInfo> routes_;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace webcc
|
|
||||||
|
|
||||||
#endif // WEBCC_REQUEST_HANDLER_H_
|
|
Loading…
Reference in New Issue