2022-01-07 20:11:06 +01:00
|
|
|
#include "web_root.h"
|
2020-11-25 00:20:41 +01:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "request.h"
|
|
|
|
|
2021-05-27 17:06:42 +02:00
|
|
|
#include "core/file_cache.h"
|
2020-11-25 00:20:41 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
#include "core/http/web_server.h"
|
|
|
|
|
|
|
|
void WebRoot::load_settings() {
|
2020-12-08 16:22:18 +01:00
|
|
|
}
|
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
void WebRoot::setup_routes() {
|
|
|
|
default_error_handler_func = WebRoot::default_fallback_error_handler;
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
error_handler_map[404] = WebRoot::default_404_error_handler;
|
2020-11-25 00:20:41 +01:00
|
|
|
}
|
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
void WebRoot::setup_middleware() {
|
2022-01-08 11:27:26 +01:00
|
|
|
// If you want sessions add this to your inherited class. Should probably be the first one.
|
|
|
|
// middlewares.push_back(HandlerInstance(::SessionManager::session_setup_middleware));
|
2021-08-04 21:58:25 +02:00
|
|
|
|
2022-01-08 11:27:26 +01:00
|
|
|
middlewares.push_back(HandlerInstance([this](Object *instance, Request *request) { this->default_routing_middleware(instance, request); }));
|
2020-11-25 00:20:41 +01:00
|
|
|
}
|
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
void WebRoot::default_routing_middleware(Object *instance, Request *request) {
|
2022-01-08 11:27:26 +01:00
|
|
|
// handle default phase 1
|
2021-07-06 20:06:38 +02:00
|
|
|
std::string path = request->get_path_full();
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2020-11-25 14:53:53 +01:00
|
|
|
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
|
2020-11-25 00:20:41 +01:00
|
|
|
send_file(path, request);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-08 11:27:26 +01:00
|
|
|
// call parent handle default
|
2022-01-08 10:50:07 +01:00
|
|
|
|
2022-01-08 11:27:26 +01:00
|
|
|
// from this this will be handled by web router node by default
|
2020-12-01 12:56:04 +01:00
|
|
|
HandlerInstance handler_data;
|
|
|
|
|
2022-01-08 11:27:26 +01:00
|
|
|
// std::function<void(Object *, Request *)> func;
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2022-01-08 11:27:26 +01:00
|
|
|
// if (path == "/") {
|
2020-12-28 19:15:27 +01:00
|
|
|
if (request->get_path_segment_count() == 0) {
|
2022-01-08 11:27:26 +01:00
|
|
|
// quick shortcut
|
2020-12-01 12:56:04 +01:00
|
|
|
handler_data = index_func;
|
2020-11-25 00:20:41 +01:00
|
|
|
} else {
|
2020-12-28 19:15:27 +01:00
|
|
|
const std::string main_route = request->get_current_path_segment();
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2020-12-01 12:56:04 +01:00
|
|
|
handler_data = main_route_map[main_route];
|
2020-12-28 19:15:27 +01:00
|
|
|
|
|
|
|
request->push_path();
|
2020-11-25 00:20:41 +01:00
|
|
|
}
|
|
|
|
|
2020-12-01 12:56:04 +01:00
|
|
|
if (!handler_data.handler_func) {
|
2020-11-25 00:20:41 +01:00
|
|
|
send_error(404, request);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-01 12:56:04 +01:00
|
|
|
request->handler_instance = handler_data;
|
2020-11-26 12:10:21 +01:00
|
|
|
request->next_stage();
|
|
|
|
}
|
|
|
|
|
2022-01-08 11:51:39 +01:00
|
|
|
void WebRoot::default_fallback_error_handler(Request *request, int error_code) {
|
2021-07-06 20:06:38 +02:00
|
|
|
request->compiled_body = default_generic_error_body;
|
2022-01-08 11:27:26 +01:00
|
|
|
|
2020-11-26 12:10:21 +01:00
|
|
|
request->send();
|
|
|
|
}
|
|
|
|
|
2022-01-08 11:51:39 +01:00
|
|
|
void WebRoot::default_404_error_handler(Request *request, int error_code) {
|
2021-07-06 20:06:38 +02:00
|
|
|
request->compiled_body = default_error_404_body;
|
2020-11-26 12:10:21 +01:00
|
|
|
request->send();
|
|
|
|
}
|
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
void WebRoot::handle_request_main(Request *request) {
|
2022-01-08 11:27:26 +01:00
|
|
|
// request->middleware_stack = &middlewares;
|
|
|
|
// note that middlewares handle the routing -> WebRoot::default_routing_middleware by default
|
|
|
|
// request->next_stage();
|
2022-01-08 10:50:07 +01:00
|
|
|
|
2022-01-08 11:27:26 +01:00
|
|
|
// handle files first
|
|
|
|
if (try_send_wwwroot_file(request)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-08 10:50:07 +01:00
|
|
|
|
2022-01-08 11:27:26 +01:00
|
|
|
// normal routing
|
|
|
|
WebRouterNode::handle_request_main(request);
|
|
|
|
}
|
2022-01-08 11:51:39 +01:00
|
|
|
|
2022-01-08 11:47:39 +01:00
|
|
|
void WebRoot::handle_error_send_request(Request *request, const int error_code) {
|
2022-01-08 11:51:39 +01:00
|
|
|
std::function<void(Request *, int)> func = error_handler_map[error_code];
|
|
|
|
|
|
|
|
if (!func) {
|
|
|
|
if (!default_error_handler_func) {
|
|
|
|
WebNode::handle_error_send_request(request, error_code);
|
|
|
|
return;
|
|
|
|
}
|
2022-01-08 11:47:39 +01:00
|
|
|
|
2022-01-08 11:51:39 +01:00
|
|
|
default_error_handler_func(request, error_code);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
func(request, error_code);
|
|
|
|
}
|
2022-01-08 11:27:26 +01:00
|
|
|
|
|
|
|
bool WebRoot::try_send_wwwroot_file(Request *request) {
|
|
|
|
const String &path = request->get_path_full();
|
2022-01-08 10:50:07 +01:00
|
|
|
|
|
|
|
if (FileCache::get_singleton()->wwwroot_has_file(path)) {
|
|
|
|
send_file(path, request);
|
|
|
|
|
2022-01-08 11:27:26 +01:00
|
|
|
return true;
|
2022-01-08 10:50:07 +01:00
|
|
|
}
|
|
|
|
|
2022-01-08 11:27:26 +01:00
|
|
|
return false;
|
2020-11-25 00:20:41 +01:00
|
|
|
}
|
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
void WebRoot::send_error(int error_code, Request *request) {
|
2020-11-25 00:20:41 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
void WebRoot::send_file(const std::string &path, Request *request) {
|
2020-11-25 14:53:53 +01:00
|
|
|
std::string fp = FileCache::get_singleton()->wwwroot + path;
|
2020-11-25 21:00:22 +01:00
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
request->send_file(fp);
|
|
|
|
}
|
2020-11-25 13:22:43 +01:00
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
void WebRoot::migrate() {
|
2021-02-09 01:25:24 +01:00
|
|
|
}
|
2020-11-25 13:22:43 +01:00
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
void WebRoot::register_request_update(Request *request) {
|
2021-02-09 01:25:24 +01:00
|
|
|
std::lock_guard<std::mutex> lock(_update_registered_requests_mutex);
|
2020-11-25 13:22:43 +01:00
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
_update_registered_requests.push_back(request);
|
|
|
|
}
|
2022-01-07 20:11:06 +01:00
|
|
|
void WebRoot::unregister_request_update(Request *request) {
|
2021-02-09 01:25:24 +01:00
|
|
|
std::lock_guard<std::mutex> lock(_update_registered_requests_mutex);
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
std::size_t s = _update_registered_requests.size();
|
|
|
|
for (std::size_t i = 0; i < s; ++i) {
|
|
|
|
Request *r = _update_registered_requests[i];
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
if (r == request) {
|
|
|
|
_update_registered_requests[i] = _update_registered_requests[s - 1];
|
2020-11-25 00:20:41 +01:00
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
_update_registered_requests.pop_back();
|
2020-11-25 13:22:43 +01:00
|
|
|
|
2021-02-09 01:25:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 00:20:41 +01:00
|
|
|
}
|
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
void WebRoot::update() {
|
2021-02-09 01:25:24 +01:00
|
|
|
for (std::size_t i = 0; i < _update_registered_requests.size(); ++i) {
|
|
|
|
Request *r = _update_registered_requests[i];
|
|
|
|
|
|
|
|
r->update();
|
|
|
|
}
|
2020-12-01 23:37:10 +01:00
|
|
|
}
|
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
WebServer *WebRoot::get_server() {
|
2022-01-08 11:27:26 +01:00
|
|
|
// todo this shoult probably be cached
|
2022-01-07 20:11:06 +01:00
|
|
|
return Object::cast_to<WebServer>(get_tree());
|
|
|
|
}
|
|
|
|
|
2022-01-08 11:27:26 +01:00
|
|
|
WebRoot::WebRoot() :
|
|
|
|
WebRouterNode() {
|
2020-11-25 00:20:41 +01:00
|
|
|
}
|
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
WebRoot::~WebRoot() {
|
2020-11-25 00:20:41 +01:00
|
|
|
main_route_map.clear();
|
|
|
|
error_handler_map.clear();
|
|
|
|
middlewares.clear();
|
|
|
|
}
|
|
|
|
|
2022-01-07 20:11:06 +01:00
|
|
|
std::string WebRoot::default_error_404_body = "<html><body>404 :(</body></html>";
|
|
|
|
std::string WebRoot::default_generic_error_body = "<html><body>Internal server error! :(</body></html>";
|