2022-06-25 01:55:54 +02:00
|
|
|
#include "web_root.h"
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
#include "web_server_request.h"
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
#include "../file_cache.h"
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
#include "web_permission.h"
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
String WebRoot::get_default_error_404_body() {
|
|
|
|
return default_error_404_body;
|
|
|
|
}
|
|
|
|
void WebRoot::set_default_error_404_body(const String &val) {
|
|
|
|
default_error_404_body = val;
|
|
|
|
}
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
String WebRoot::get_default_generic_error_body() {
|
|
|
|
return default_generic_error_body;
|
|
|
|
}
|
|
|
|
void WebRoot::set_default_generic_error_body(const String &val) {
|
|
|
|
default_generic_error_body = val;
|
|
|
|
}
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
void WebRoot::setup() {
|
|
|
|
setup_error_handlers();
|
|
|
|
setup_middleware();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebRoot::setup_error_handlers() {
|
|
|
|
default_error_handler_func = WebRoot::default_fallback_error_handler;
|
|
|
|
error_handler_map[404] = WebRoot::default_404_error_handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebRoot::setup_middleware() {
|
2022-06-26 15:00:25 +02:00
|
|
|
// WebServerMiddlewares get processed in the order they are in the _middlewares array
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
// ------- Built in middleware selection -------
|
|
|
|
|
2022-06-26 15:00:25 +02:00
|
|
|
// --- SessionSetupWebServerMiddleware ---
|
2022-06-25 01:55:54 +02:00
|
|
|
// If you want sessions add this to your inherited class.
|
|
|
|
// (#include "web/http/session_manager.h")
|
|
|
|
|
2022-06-26 15:00:25 +02:00
|
|
|
// _middlewares.push_back(Ref<SessionSetupWebServerMiddleware>(new SessionSetupWebServerMiddleware()));
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-26 15:00:25 +02:00
|
|
|
// --- UserSessionSetupWebServerMiddleware ---
|
2022-06-25 01:55:54 +02:00
|
|
|
// This one looks up users based on sessions
|
|
|
|
// (#include "web_modules/users/user_controller.h")
|
|
|
|
|
2022-06-26 15:00:25 +02:00
|
|
|
// _middlewares.push_back(Ref<UserSessionSetupWebServerMiddleware>(new UserSessionSetupWebServerMiddleware()));
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-26 15:00:25 +02:00
|
|
|
// --- RBACUserSessionSetupWebServerMiddleware / RBACDefaultUserSessionSetupWebServerMiddleware ---
|
2022-06-25 01:55:54 +02:00
|
|
|
// Same as the previous, but if you want the RBAC system to work use one of these
|
2022-06-26 15:00:25 +02:00
|
|
|
// UserSessionSetupWebServerMiddleware is not needed if you need these
|
2022-06-25 01:55:54 +02:00
|
|
|
// (#include "web_modules/rbac_users/rbac_user_controller.h")
|
|
|
|
|
2022-06-26 15:00:25 +02:00
|
|
|
// _middlewares.push_back(Ref<RBACUserSessionSetupWebServerMiddleware>(new RBACUserSessionSetupWebServerMiddleware()));
|
|
|
|
// _middlewares.push_back(Ref<RBACDefaultUserSessionSetupWebServerMiddleware>(new RBACDefaultUserSessionSetupWebServerMiddleware()));
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
// --- CSRF Token Middlweare ---
|
|
|
|
// (#include "web/http/csrf_token.h")
|
|
|
|
|
2022-06-26 15:00:25 +02:00
|
|
|
// Ref<CSRFTokenWebServerMiddleware> csrf_middleware;
|
2022-06-25 01:55:54 +02:00
|
|
|
// csrf_middleware.instance();
|
|
|
|
// csrf_middleware->ignored_urls.push_back("/user/login");
|
|
|
|
// csrf_middleware->ignored_urls.push_back("/user/register");
|
|
|
|
// _middlewares.push_back(csrf_middleware);
|
|
|
|
}
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
void WebRoot::default_fallback_error_handler(Ref<WebServerRequest> request, int error_code) {
|
2022-06-25 01:55:54 +02:00
|
|
|
request->compiled_body = default_generic_error_body;
|
|
|
|
request->send();
|
|
|
|
}
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
void WebRoot::default_404_error_handler(Ref<WebServerRequest> request, int error_code) {
|
2022-06-25 01:55:54 +02:00
|
|
|
request->compiled_body = default_error_404_body;
|
|
|
|
request->send();
|
|
|
|
}
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
void WebRoot::handle_request_main(Ref<WebServerRequest> request) {
|
2022-06-25 01:55:54 +02:00
|
|
|
if (process_middlewares(request)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_web_permission.is_valid()) {
|
|
|
|
if (_web_permission->activate(request)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle files first
|
|
|
|
if (try_send_wwwroot_file(request)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// normal routing
|
|
|
|
if (!_routing_enabled) {
|
|
|
|
_handle_request_main(request);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!try_route_request_to_children(request)) {
|
|
|
|
_handle_request_main(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
void WebRoot::handle_error_send_request(Ref<WebServerRequest> request, const int error_code) {
|
|
|
|
void (*func)(Ref<WebServerRequest>, int) = error_handler_map[error_code];
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
if (!func) {
|
|
|
|
if (!default_error_handler_func) {
|
|
|
|
WebNode::handle_error_send_request(request, error_code);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
default_error_handler_func(request, error_code);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
func(request, error_code);
|
|
|
|
}
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
bool WebRoot::process_middlewares(Ref<WebServerRequest> request) {
|
2022-06-25 01:55:54 +02:00
|
|
|
for (int i = 0; i < _middlewares.size(); ++i) {
|
2022-06-27 01:15:01 +02:00
|
|
|
if (_middlewares.write[i]->on_before_handle_request_main(request)) {
|
2022-06-25 01:55:54 +02:00
|
|
|
// handled
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
bool WebRoot::try_send_wwwroot_file(Ref<WebServerRequest> request) {
|
2022-06-25 01:55:54 +02:00
|
|
|
const String &path = request->get_path_full();
|
|
|
|
|
|
|
|
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
|
|
|
|
send_file(path, request);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
void WebRoot::send_file(const String &path, Ref<WebServerRequest> request) {
|
2022-06-25 01:55:54 +02:00
|
|
|
String fp = FileCache::get_singleton()->wwwroot + path;
|
|
|
|
|
|
|
|
request->send_file(fp);
|
|
|
|
}
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
void WebRoot::register_request_update(Ref<WebServerRequest> request) {
|
|
|
|
_update_registered_requests_mutex.lock();
|
2022-06-25 01:55:54 +02:00
|
|
|
_update_registered_requests.push_back(request);
|
2022-06-27 01:15:01 +02:00
|
|
|
_update_registered_requests_mutex.unlock();
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
2022-06-27 01:15:01 +02:00
|
|
|
void WebRoot::unregister_request_update(Ref<WebServerRequest> request) {
|
|
|
|
_update_registered_requests_mutex.lock();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
std::size_t s = _update_registered_requests.size();
|
|
|
|
for (std::size_t i = 0; i < s; ++i) {
|
2022-06-27 01:15:01 +02:00
|
|
|
Ref<WebServerRequest> r = _update_registered_requests[i];
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
if (r == request) {
|
2022-06-27 01:15:01 +02:00
|
|
|
_update_registered_requests.remove(i);
|
|
|
|
_update_registered_requests_mutex.unlock();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-06-27 01:15:01 +02:00
|
|
|
_update_registered_requests_mutex.unlock();
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebRoot::update() {
|
|
|
|
for (std::size_t i = 0; i < _update_registered_requests.size(); ++i) {
|
2022-06-27 01:15:01 +02:00
|
|
|
Ref<WebServerRequest> r = _update_registered_requests[i];
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
r->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
WebRoot::WebRoot() {
|
|
|
|
default_error_handler_func = nullptr;
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WebRoot::~WebRoot() {
|
|
|
|
error_handler_map.clear();
|
|
|
|
_middlewares.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
String WebRoot::default_error_404_body = "<html><body>404 :(</body></html>";
|
|
|
|
String WebRoot::default_generic_error_body = "<html><body>Internal server error! :(</body></html>";
|