2023-12-18 00:25:33 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* web_root.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* PANDEMONIUM ENGINE */
|
|
|
|
/* https://github.com/Relintai/pandemonium_engine */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2022-present Péter Magyar. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
|
2022-06-25 01:55:54 +02:00
|
|
|
#include "web_root.h"
|
|
|
|
|
2022-08-17 13:45:14 +02:00
|
|
|
#include "core/object/object.h"
|
2022-06-27 13:52:33 +02:00
|
|
|
#include "modules/web/http/http_server_enums.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 13:52:33 +02:00
|
|
|
String WebRoot::get_www_root_path() {
|
|
|
|
return _www_root_path;
|
2022-06-27 01:15:01 +02:00
|
|
|
}
|
2022-06-27 13:52:33 +02:00
|
|
|
void WebRoot::set_www_root_path(const String &val) {
|
|
|
|
_www_root_path = val;
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
if (val == "") {
|
2022-07-01 22:13:45 +02:00
|
|
|
_www_root_file_cache->set_wwwroot(val);
|
2022-06-27 13:52:33 +02:00
|
|
|
_www_root_file_cache->clear();
|
|
|
|
} else {
|
2022-07-01 22:13:45 +02:00
|
|
|
_www_root_file_cache->set_wwwroot(val);
|
|
|
|
_www_root_file_cache->wwwroot_refresh_cache();
|
2022-06-27 13:52:33 +02:00
|
|
|
}
|
2022-06-27 01:15:01 +02:00
|
|
|
}
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
Ref<FileCache> WebRoot::get_www_root_file_cache() {
|
|
|
|
return _www_root_file_cache;
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
Vector<Variant> WebRoot::get_middlewares() {
|
|
|
|
Vector<Variant> r;
|
|
|
|
for (int i = 0; i < _middlewares.size(); i++) {
|
|
|
|
r.push_back(_middlewares[i].get_ref_ptr());
|
|
|
|
}
|
|
|
|
return r;
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
void WebRoot::set_middlewares(const Vector<Variant> &data) {
|
|
|
|
_middlewares.clear();
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
Ref<WebServerMiddleware> e = Ref<WebServerMiddleware>(data[i]);
|
|
|
|
_middlewares.push_back(e);
|
|
|
|
}
|
|
|
|
}
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
//void WebRoot::setup_middleware() {
|
|
|
|
// WebServerMiddlewares get processed in the order they are in the _middlewares array
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
// ------- Built in middleware selection -------
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
// --- SessionSetupWebServerMiddleware ---
|
|
|
|
// If you want sessions add this to your inherited class.
|
|
|
|
// (#include "web/http/session_manager.h")
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
// _middlewares.push_back(Ref<SessionSetupWebServerMiddleware>(new SessionSetupWebServerMiddleware()));
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
// --- UserSessionSetupWebServerMiddleware ---
|
|
|
|
// This one looks up users based on sessions
|
|
|
|
// (#include "web_modules/users/user_controller.h")
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
// _middlewares.push_back(Ref<UserSessionSetupWebServerMiddleware>(new UserSessionSetupWebServerMiddleware()));
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
// --- RBACUserSessionSetupWebServerMiddleware / RBACDefaultUserSessionSetupWebServerMiddleware ---
|
|
|
|
// Same as the previous, but if you want the RBAC system to work use one of these
|
|
|
|
// UserSessionSetupWebServerMiddleware is not needed if you need these
|
|
|
|
// (#include "web_modules/rbac_users/rbac_user_controller.h")
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
// _middlewares.push_back(Ref<RBACUserSessionSetupWebServerMiddleware>(new RBACUserSessionSetupWebServerMiddleware()));
|
|
|
|
// _middlewares.push_back(Ref<RBACDefaultUserSessionSetupWebServerMiddleware>(new RBACDefaultUserSessionSetupWebServerMiddleware()));
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
// --- CSRF Token Middlweare ---
|
|
|
|
// (#include "web/http/csrf_token.h")
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
// Ref<CSRFTokenWebServerMiddleware> csrf_middleware;
|
|
|
|
// 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-25 01:55:54 +02:00
|
|
|
|
2022-06-27 13:52:33 +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) {
|
2022-06-27 15:42:57 +02:00
|
|
|
handle_request(request);
|
2022-06-25 01:55:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!try_route_request_to_children(request)) {
|
2022-06-27 15:42:57 +02:00
|
|
|
handle_request(request);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
void WebRoot::_handle_error_send_request(Ref<WebServerRequest> request, const int error_code) {
|
|
|
|
if (error_code == HTTPServerEnums::HTTP_STATUS_CODE_404_NOT_FOUND) {
|
|
|
|
request->compiled_body = "<html><body>404 :(</body></html>";
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
request->send();
|
2022-06-25 01:55:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
request->compiled_body = "<html><body>Internal server error! :(</body></html>";
|
|
|
|
request->send();
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
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 13:52:33 +02:00
|
|
|
Ref<WebServerMiddleware> m = _middlewares[i];
|
|
|
|
|
|
|
|
if (!m.is_valid()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->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-07-01 18:50:56 +02:00
|
|
|
String path = request->get_path_full();
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2023-12-22 19:29:48 +01:00
|
|
|
String file_path = _www_root_file_cache->wwwroot_get_file_abspath(path);
|
2022-07-01 22:13:45 +02:00
|
|
|
|
2023-12-22 19:29:48 +01:00
|
|
|
if (!file_path.empty()) {
|
|
|
|
request->send_file(file_path);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
void WebRoot::send_file(const String &path, Ref<WebServerRequest> request) {
|
2022-07-01 22:13:45 +02:00
|
|
|
String fp = _www_root_file_cache->get_wwwroot_abs() + path;
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
WebRoot::WebRoot() {
|
2022-06-27 13:52:33 +02:00
|
|
|
_www_root_file_cache.instance();
|
|
|
|
set_process_internal(true);
|
2022-06-25 01:55:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WebRoot::~WebRoot() {
|
|
|
|
}
|
|
|
|
|
2022-06-27 13:52:33 +02:00
|
|
|
void WebRoot::_notification(int p_what) {
|
|
|
|
if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
|
|
|
|
for (int i = 0; i < _update_registered_requests.size(); ++i) {
|
|
|
|
Ref<WebServerRequest> r = _update_registered_requests[i];
|
|
|
|
|
|
|
|
r->update();
|
|
|
|
}
|
2023-03-22 14:29:09 +01:00
|
|
|
} else if (p_what == NOTIFICATION_READY) {
|
|
|
|
if (!_www_root_path.empty()) {
|
|
|
|
_www_root_file_cache->wwwroot_refresh_cache();
|
|
|
|
}
|
2022-06-27 13:52:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebRoot::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_www_root_path"), &WebRoot::get_www_root_path);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_www_root_path", "val"), &WebRoot::set_www_root_path);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "www_root_path"), "set_www_root_path", "get_www_root_path");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_www_root_file_cache"), &WebRoot::get_www_root_file_cache);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "www_root_file_cache", PROPERTY_HINT_RESOURCE_TYPE, "FileCache", 0), "", "get_www_root_file_cache");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_middlewares"), &WebRoot::get_middlewares);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_middlewares", "data"), &WebRoot::set_middlewares);
|
2023-06-10 20:58:49 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "middlewares", PROPERTY_HINT_NONE, "23/20:WebServerMiddleware", PROPERTY_USAGE_DEFAULT, "WebServerMiddleware"), "set_middlewares", "get_middlewares");
|
2022-06-27 13:52:33 +02:00
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("process_middlewares", "request"), &WebRoot::process_middlewares);
|
|
|
|
ClassDB::bind_method(D_METHOD("try_send_wwwroot_file", "request"), &WebRoot::try_send_wwwroot_file);
|
|
|
|
ClassDB::bind_method(D_METHOD("send_file", "path", "request"), &WebRoot::send_file);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("register_request_update", "request"), &WebRoot::register_request_update);
|
|
|
|
ClassDB::bind_method(D_METHOD("unregister_request_update", "request"), &WebRoot::unregister_request_update);
|
|
|
|
}
|