mirror of
https://github.com/Relintai/pandemonium_engine.git
synced 2025-01-24 18:17:21 +01:00
Cleaned up WebRoot aswell.
This commit is contained in:
parent
7aa6798ab7
commit
ba1ea3c62a
@ -1,17 +1,24 @@
|
||||
#include "web_root.h"
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include "web_server_request.h"
|
||||
|
||||
#include "request.h"
|
||||
|
||||
#include "web/file_cache.h"
|
||||
|
||||
#include <iostream>
|
||||
#include "../file_cache.h"
|
||||
|
||||
#include "web_permission.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void WebRoot::setup() {
|
||||
setup_error_handlers();
|
||||
@ -58,17 +65,17 @@ void WebRoot::setup_middleware() {
|
||||
// _middlewares.push_back(csrf_middleware);
|
||||
}
|
||||
|
||||
void WebRoot::default_fallback_error_handler(Request *request, int error_code) {
|
||||
void WebRoot::default_fallback_error_handler(Ref<WebServerRequest> request, int error_code) {
|
||||
request->compiled_body = default_generic_error_body;
|
||||
request->send();
|
||||
}
|
||||
|
||||
void WebRoot::default_404_error_handler(Request *request, int error_code) {
|
||||
void WebRoot::default_404_error_handler(Ref<WebServerRequest> request, int error_code) {
|
||||
request->compiled_body = default_error_404_body;
|
||||
request->send();
|
||||
}
|
||||
|
||||
void WebRoot::handle_request_main(Request *request) {
|
||||
void WebRoot::handle_request_main(Ref<WebServerRequest> request) {
|
||||
if (process_middlewares(request)) {
|
||||
return;
|
||||
}
|
||||
@ -95,8 +102,8 @@ void WebRoot::handle_request_main(Request *request) {
|
||||
}
|
||||
}
|
||||
|
||||
void WebRoot::handle_error_send_request(Request *request, const int error_code) {
|
||||
std::function<void(Request *, int)> func = error_handler_map[error_code];
|
||||
void WebRoot::handle_error_send_request(Ref<WebServerRequest> request, const int error_code) {
|
||||
void (*func)(Ref<WebServerRequest>, int) = error_handler_map[error_code];
|
||||
|
||||
if (!func) {
|
||||
if (!default_error_handler_func) {
|
||||
@ -111,9 +118,9 @@ void WebRoot::handle_error_send_request(Request *request, const int error_code)
|
||||
func(request, error_code);
|
||||
}
|
||||
|
||||
bool WebRoot::process_middlewares(Request *request) {
|
||||
bool WebRoot::process_middlewares(Ref<WebServerRequest> request) {
|
||||
for (int i = 0; i < _middlewares.size(); ++i) {
|
||||
if (_middlewares[i]->on_before_handle_request_main(request)) {
|
||||
if (_middlewares.write[i]->on_before_handle_request_main(request)) {
|
||||
// handled
|
||||
return true;
|
||||
}
|
||||
@ -122,7 +129,7 @@ bool WebRoot::process_middlewares(Request *request) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool WebRoot::try_send_wwwroot_file(Request *request) {
|
||||
bool WebRoot::try_send_wwwroot_file(Ref<WebServerRequest> request) {
|
||||
const String &path = request->get_path_full();
|
||||
|
||||
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
|
||||
@ -134,44 +141,44 @@ bool WebRoot::try_send_wwwroot_file(Request *request) {
|
||||
return false;
|
||||
}
|
||||
|
||||
void WebRoot::send_file(const String &path, Request *request) {
|
||||
void WebRoot::send_file(const String &path, Ref<WebServerRequest> request) {
|
||||
String fp = FileCache::get_singleton()->wwwroot + path;
|
||||
|
||||
request->send_file(fp);
|
||||
}
|
||||
|
||||
void WebRoot::register_request_update(Request *request) {
|
||||
std::lock_guard<std::mutex> lock(_update_registered_requests_mutex);
|
||||
|
||||
void WebRoot::register_request_update(Ref<WebServerRequest> request) {
|
||||
_update_registered_requests_mutex.lock();
|
||||
_update_registered_requests.push_back(request);
|
||||
_update_registered_requests_mutex.unlock();
|
||||
}
|
||||
void WebRoot::unregister_request_update(Request *request) {
|
||||
std::lock_guard<std::mutex> lock(_update_registered_requests_mutex);
|
||||
void WebRoot::unregister_request_update(Ref<WebServerRequest> request) {
|
||||
_update_registered_requests_mutex.lock();
|
||||
|
||||
std::size_t s = _update_registered_requests.size();
|
||||
for (std::size_t i = 0; i < s; ++i) {
|
||||
Request *r = _update_registered_requests[i];
|
||||
Ref<WebServerRequest> r = _update_registered_requests[i];
|
||||
|
||||
if (r == request) {
|
||||
_update_registered_requests[i] = _update_registered_requests[s - 1];
|
||||
|
||||
_update_registered_requests.pop_back();
|
||||
_update_registered_requests.remove(i);
|
||||
_update_registered_requests_mutex.unlock();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
_update_registered_requests_mutex.unlock();
|
||||
}
|
||||
|
||||
void WebRoot::update() {
|
||||
for (std::size_t i = 0; i < _update_registered_requests.size(); ++i) {
|
||||
Request *r = _update_registered_requests[i];
|
||||
Ref<WebServerRequest> r = _update_registered_requests[i];
|
||||
|
||||
r->update();
|
||||
}
|
||||
}
|
||||
|
||||
WebRoot::WebRoot() :
|
||||
WebNode() {
|
||||
WebRoot::WebRoot() {
|
||||
default_error_handler_func = nullptr;
|
||||
}
|
||||
|
||||
WebRoot::~WebRoot() {
|
||||
|
@ -1,59 +1,64 @@
|
||||
#ifndef WEB_ROOT_H
|
||||
#define WEB_ROOT_H
|
||||
|
||||
#include "core/containers/vector.h"
|
||||
#include "core/string.h"
|
||||
#include "mutex"
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include "core/hash_map.h"
|
||||
#include "core/os/mutex.h"
|
||||
#include "core/ustring.h"
|
||||
#include "core/vector.h"
|
||||
|
||||
#include "web_node.h"
|
||||
|
||||
#include "middleware.h"
|
||||
#include "web_server_middleware.h"
|
||||
|
||||
class Request;
|
||||
class WebServerRequest;
|
||||
|
||||
// TODO FileCache -> set up, for this webroot, don't use singleton
|
||||
|
||||
class WebRoot : public WebNode {
|
||||
RCPP_OBJECT(WebRoot, WebNode);
|
||||
GDCLASS(WebRoot, WebNode);
|
||||
|
||||
public:
|
||||
static String default_error_404_body;
|
||||
static String default_generic_error_body;
|
||||
String get_default_error_404_body();
|
||||
void set_default_error_404_body(const String &val);
|
||||
|
||||
void handle_request_main(Request *request);
|
||||
void handle_error_send_request(Request *request, const int error_code);
|
||||
String get_default_generic_error_body();
|
||||
void set_default_generic_error_body(const String &val);
|
||||
|
||||
bool process_middlewares(Request *request);
|
||||
bool try_send_wwwroot_file(Request *request);
|
||||
void send_file(const String &path, Request *request);
|
||||
void handle_request_main(Ref<WebServerRequest> request);
|
||||
void handle_error_send_request(Ref<WebServerRequest> request, const int error_code);
|
||||
|
||||
static void default_fallback_error_handler(Request *request, int error_code);
|
||||
static void default_404_error_handler(Request *request, int error_code);
|
||||
bool process_middlewares(Ref<WebServerRequest> request);
|
||||
bool try_send_wwwroot_file(Ref<WebServerRequest> request);
|
||||
void send_file(const String &path, Ref<WebServerRequest> request);
|
||||
|
||||
static void default_fallback_error_handler(Ref<WebServerRequest> request, int error_code);
|
||||
static void default_404_error_handler(Ref<WebServerRequest> request, int error_code);
|
||||
|
||||
virtual void setup();
|
||||
virtual void setup_error_handlers();
|
||||
virtual void setup_middleware();
|
||||
|
||||
void default_routing_middleware(Object *instance, Request *request);
|
||||
void default_routing_middleware(Object *instance, Ref<WebServerRequest> request);
|
||||
|
||||
void register_request_update(Request *request);
|
||||
void unregister_request_update(Request *request);
|
||||
void register_request_update(Ref<WebServerRequest> request);
|
||||
void unregister_request_update(Ref<WebServerRequest> request);
|
||||
void update();
|
||||
|
||||
WebRoot();
|
||||
virtual ~WebRoot();
|
||||
~WebRoot();
|
||||
|
||||
static String default_error_404_body;
|
||||
static String default_generic_error_body;
|
||||
|
||||
public:
|
||||
Vector<Ref<WebServerMiddleware>> _middlewares;
|
||||
|
||||
std::map<int, std::function<void(Request *, int)>> error_handler_map;
|
||||
std::function<void(Request *, int)> default_error_handler_func;
|
||||
HashMap<int, void (*)(Ref<WebServerRequest>, int)> error_handler_map;
|
||||
void (*default_error_handler_func)(Ref<WebServerRequest>, int);
|
||||
|
||||
protected:
|
||||
std::mutex _update_registered_requests_mutex;
|
||||
std::vector<Request *> _update_registered_requests;
|
||||
Mutex _update_registered_requests_mutex;
|
||||
Vector<Ref<WebServerRequest>> _update_registered_requests;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user