2022-01-07 20:11:06 +01:00
|
|
|
#ifndef WEB_ROOT_H
|
|
|
|
#define WEB_ROOT_H
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2022-01-08 12:25:52 +01:00
|
|
|
#include "core/containers/vector.h"
|
|
|
|
#include "mutex"
|
2020-11-25 00:20:41 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
2022-01-08 12:41:15 +01:00
|
|
|
#include "core/string.h"
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2022-02-04 20:15:45 +01:00
|
|
|
#include "web_node.h"
|
2021-02-09 01:25:24 +01:00
|
|
|
|
2022-01-08 12:25:52 +01:00
|
|
|
#include "middleware.h"
|
2020-12-01 12:56:04 +01:00
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
class Request;
|
|
|
|
|
2022-01-08 12:49:34 +01:00
|
|
|
// TODO FileCache -> set up, for this webroot, don't use singleton
|
2022-01-08 10:50:07 +01:00
|
|
|
|
2022-02-04 20:15:45 +01:00
|
|
|
class WebRoot : public WebNode {
|
|
|
|
RCPP_OBJECT(WebRoot, WebNode);
|
2021-11-21 14:40:47 +01:00
|
|
|
|
2020-11-25 00:20:41 +01:00
|
|
|
public:
|
2022-01-08 12:41:15 +01:00
|
|
|
static String default_error_404_body;
|
|
|
|
static String default_generic_error_body;
|
2020-11-26 11:32:33 +01:00
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
void handle_request_main(Request *request);
|
2022-01-08 11:51:39 +01:00
|
|
|
void handle_error_send_request(Request *request, const int error_code);
|
2022-01-08 11:47:39 +01:00
|
|
|
|
2022-01-09 16:03:27 +01:00
|
|
|
bool process_middlewares(Request *request);
|
2022-01-08 11:27:26 +01:00
|
|
|
bool try_send_wwwroot_file(Request *request);
|
2022-01-08 12:41:15 +01:00
|
|
|
void send_file(const String &path, Request *request);
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2022-01-08 11:51:39 +01:00
|
|
|
static void default_fallback_error_handler(Request *request, int error_code);
|
|
|
|
static void default_404_error_handler(Request *request, int error_code);
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2022-01-08 12:35:44 +01:00
|
|
|
virtual void setup();
|
|
|
|
virtual void setup_error_handlers();
|
2020-12-01 12:56:04 +01:00
|
|
|
virtual void setup_middleware();
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2021-04-28 22:58:13 +02:00
|
|
|
void default_routing_middleware(Object *instance, Request *request);
|
2020-11-26 12:10:21 +01:00
|
|
|
|
2021-04-28 22:58:13 +02:00
|
|
|
void register_request_update(Request *request);
|
|
|
|
void unregister_request_update(Request *request);
|
|
|
|
void update();
|
2021-02-09 01:25:24 +01:00
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
WebRoot();
|
|
|
|
virtual ~WebRoot();
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2020-12-01 12:56:04 +01:00
|
|
|
public:
|
2022-01-08 12:25:52 +01:00
|
|
|
Vector<Ref<Middleware> > _middlewares;
|
|
|
|
|
2022-01-08 11:51:39 +01:00
|
|
|
std::map<int, std::function<void(Request *, int)> > error_handler_map;
|
|
|
|
std::function<void(Request *, int)> default_error_handler_func;
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
protected:
|
2021-04-28 22:58:13 +02:00
|
|
|
std::mutex _update_registered_requests_mutex;
|
|
|
|
std::vector<Request *> _update_registered_requests;
|
2020-11-25 00:20:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|