rcpp_framework/core/http/web_root.cpp

148 lines
3.6 KiB
C++
Raw Normal View History

#include "web_root.h"
2020-11-25 00:20:41 +01:00
#include <functional>
#include <string>
#include "request.h"
#include "core/file_cache.h"
2020-11-25 00:20:41 +01:00
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
void WebRoot::load_settings() {
2020-12-08 16:22:18 +01:00
}
void WebRoot::setup_routes() {
default_error_handler_func = WebRoot::default_fallback_error_handler;
2020-11-25 00:20:41 +01:00
error_handler_map[404] = WebRoot::default_404_error_handler;
2020-11-25 00:20:41 +01:00
}
void WebRoot::setup_middleware() {
// Middlewares get processed in the order they are in the _middlewares array
// If you want sessions add this to your inherited class.
// _middlewares.push_back(Ref<SessionSetupMiddleware>(new SessionSetupMiddleware()));
2020-11-25 00:20:41 +01:00
// This one looks up users based on sessions
// _middlewares.push_back(Ref<UserSessionSetupMiddleware>(new UserSessionSetupMiddleware()));
2020-11-25 00:20:41 +01:00
// Same as the previous, but if you want the RBAC system to work use one of these
// _middlewares.push_back(Ref<RBACUserSessionSetupMiddleware>(new RBACUserSessionSetupMiddleware()));
// _middlewares.push_back(Ref<RBACDefaultUserSessionSetupMiddleware>(new RBACDefaultUserSessionSetupMiddleware()));
}
void WebRoot::default_fallback_error_handler(Request *request, int error_code) {
request->compiled_body = default_generic_error_body;
request->send();
}
void WebRoot::default_404_error_handler(Request *request, int error_code) {
request->compiled_body = default_error_404_body;
request->send();
}
void WebRoot::handle_request_main(Request *request) {
for (int i = 0; i < _middlewares.size(); ++i) {
if (_middlewares[i]->on_before_handle_request_main(request)) {
//handled
return;
}
}
2022-01-08 10:50:07 +01:00
// handle files first
if (try_send_wwwroot_file(request)) {
return;
}
2022-01-08 10:50:07 +01:00
// normal routing
WebRouterNode::handle_request_main(request);
}
2022-01-08 11:47:39 +01:00
void WebRoot::handle_error_send_request(Request *request, const int error_code) {
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
default_error_handler_func(request, error_code);
return;
}
func(request, error_code);
}
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);
return true;
2022-01-08 10:50:07 +01:00
}
return false;
2020-11-25 00:20:41 +01:00
}
void WebRoot::send_error(int error_code, Request *request) {
2020-11-25 00:20:41 +01:00
}
void WebRoot::send_file(const std::string &path, Request *request) {
std::string fp = FileCache::get_singleton()->wwwroot + path;
2020-11-25 21:00:22 +01:00
request->send_file(fp);
}
void WebRoot::migrate() {
}
void WebRoot::register_request_update(Request *request) {
std::lock_guard<std::mutex> lock(_update_registered_requests_mutex);
_update_registered_requests.push_back(request);
}
void WebRoot::unregister_request_update(Request *request) {
std::lock_guard<std::mutex> lock(_update_registered_requests_mutex);
2020-11-25 00:20:41 +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
if (r == request) {
_update_registered_requests[i] = _update_registered_requests[s - 1];
2020-11-25 00:20:41 +01:00
_update_registered_requests.pop_back();
return;
}
}
2020-11-25 00:20:41 +01:00
}
void WebRoot::update() {
for (std::size_t i = 0; i < _update_registered_requests.size(); ++i) {
Request *r = _update_registered_requests[i];
r->update();
}
}
WebRoot::WebRoot() :
WebRouterNode() {
2020-11-25 00:20:41 +01:00
}
WebRoot::~WebRoot() {
2020-11-25 00:20:41 +01:00
main_route_map.clear();
error_handler_map.clear();
middlewares.clear();
}
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>";