Move middleware processing to a helper method.

This commit is contained in:
Relintai 2022-01-08 13:09:12 +01:00
parent 7dc547b022
commit d380d14152
2 changed files with 12 additions and 7 deletions

View File

@ -25,7 +25,7 @@ void WebRoot::setup_error_handlers() {
void WebRoot::setup_middleware() { void WebRoot::setup_middleware() {
// Middlewares get processed in the order they are in the _middlewares array // Middlewares get processed in the order they are in the _middlewares array
// If you want sessions add this to your inherited class. // If you want sessions add this to your inherited class.
// _middlewares.push_back(Ref<SessionSetupMiddleware>(new SessionSetupMiddleware())); // _middlewares.push_back(Ref<SessionSetupMiddleware>(new SessionSetupMiddleware()));
// This one looks up users based on sessions // This one looks up users based on sessions
@ -47,12 +47,7 @@ void WebRoot::default_404_error_handler(Request *request, int error_code) {
} }
void WebRoot::handle_request_main(Request *request) { void WebRoot::handle_request_main(Request *request) {
for (int i = 0; i < _middlewares.size(); ++i) { process_middlewares(request);
if (_middlewares[i]->on_before_handle_request_main(request)) {
//handled
return;
}
}
// handle files first // handle files first
if (try_send_wwwroot_file(request)) { if (try_send_wwwroot_file(request)) {
@ -79,6 +74,15 @@ void WebRoot::handle_error_send_request(Request *request, const int error_code)
func(request, error_code); func(request, error_code);
} }
void WebRoot::process_middlewares(Request *request) {
for (int i = 0; i < _middlewares.size(); ++i) {
if (_middlewares[i]->on_before_handle_request_main(request)) {
// handled
return;
}
}
}
bool WebRoot::try_send_wwwroot_file(Request *request) { bool WebRoot::try_send_wwwroot_file(Request *request) {
const String &path = request->get_path_full(); const String &path = request->get_path_full();

View File

@ -25,6 +25,7 @@ public:
void handle_request_main(Request *request); void handle_request_main(Request *request);
void handle_error_send_request(Request *request, const int error_code); void handle_error_send_request(Request *request, const int error_code);
void process_middlewares(Request *request);
bool try_send_wwwroot_file(Request *request); bool try_send_wwwroot_file(Request *request);
void send_file(const String &path, Request *request); void send_file(const String &path, Request *request);