2022-06-25 01:55:54 +02:00
|
|
|
#ifndef WEB_ROOT_H
|
|
|
|
#define WEB_ROOT_H
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
#include "core/hash_map.h"
|
|
|
|
#include "core/os/mutex.h"
|
|
|
|
#include "core/ustring.h"
|
|
|
|
#include "core/vector.h"
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
#include "web_node.h"
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
#include "web_server_middleware.h"
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
class WebServerRequest;
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
// TODO FileCache -> set up, for this webroot, don't use singleton
|
|
|
|
|
|
|
|
class WebRoot : public WebNode {
|
2022-06-27 01:15:01 +02:00
|
|
|
GDCLASS(WebRoot, WebNode);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
public:
|
2022-06-27 01:15:01 +02:00
|
|
|
String get_default_error_404_body();
|
|
|
|
void set_default_error_404_body(const String &val);
|
|
|
|
|
|
|
|
String get_default_generic_error_body();
|
|
|
|
void set_default_generic_error_body(const String &val);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
void handle_request_main(Ref<WebServerRequest> request);
|
|
|
|
void handle_error_send_request(Ref<WebServerRequest> request, const int error_code);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
bool process_middlewares(Ref<WebServerRequest> request);
|
|
|
|
bool try_send_wwwroot_file(Ref<WebServerRequest> request);
|
|
|
|
void send_file(const String &path, Ref<WebServerRequest> request);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
static void default_fallback_error_handler(Ref<WebServerRequest> request, int error_code);
|
|
|
|
static void default_404_error_handler(Ref<WebServerRequest> request, int error_code);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
virtual void setup();
|
|
|
|
virtual void setup_error_handlers();
|
|
|
|
virtual void setup_middleware();
|
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
void default_routing_middleware(Object *instance, Ref<WebServerRequest> request);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
void register_request_update(Ref<WebServerRequest> request);
|
|
|
|
void unregister_request_update(Ref<WebServerRequest> request);
|
2022-06-25 01:55:54 +02:00
|
|
|
void update();
|
|
|
|
|
|
|
|
WebRoot();
|
2022-06-27 01:15:01 +02:00
|
|
|
~WebRoot();
|
|
|
|
|
|
|
|
static String default_error_404_body;
|
|
|
|
static String default_generic_error_body;
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
public:
|
2022-06-26 15:00:25 +02:00
|
|
|
Vector<Ref<WebServerMiddleware>> _middlewares;
|
2022-06-25 01:55:54 +02:00
|
|
|
|
2022-06-27 01:15:01 +02:00
|
|
|
HashMap<int, void (*)(Ref<WebServerRequest>, int)> error_handler_map;
|
|
|
|
void (*default_error_handler_func)(Ref<WebServerRequest>, int);
|
2022-06-25 01:55:54 +02:00
|
|
|
|
|
|
|
protected:
|
2022-06-27 01:15:01 +02:00
|
|
|
Mutex _update_registered_requests_mutex;
|
|
|
|
Vector<Ref<WebServerRequest>> _update_registered_requests;
|
2022-06-25 01:55:54 +02:00
|
|
|
};
|
|
|
|
|
2022-06-26 15:00:25 +02:00
|
|
|
#endif
|