diff --git a/core/application.cpp b/core/application.cpp index 6a546da..2fa0664 100644 --- a/core/application.cpp +++ b/core/application.cpp @@ -23,13 +23,11 @@ void Application::setup_middleware() { void Application::default_fallback_error_handler(int error_code, Request *request) { request->response->setBody(default_generic_error_body); - request->finalized = true; request->send(); } void Application::default_404_error_handler(int error_code, Request *request) { request->response->setBody(default_error_404_body); - request->finalized = true; request->send(); } @@ -69,16 +67,10 @@ void Application::handle_request(Request *request) { return; } - for (uint32_t i = 0; i < middlewares.size(); ++i) { - middlewares[i](request); + request->handler_func = func; + request->middleware_stack = &middlewares; - if (request->finalized) { - request->send(); - return; - } - } - - func(request); + request->next_stage(); } void Application::send_error(int error_code, Request *request) { @@ -117,7 +109,6 @@ void Application::send_file(const std::string &path, Request *request) { //TODO set mimetype? request->response->setBody(body); - request->finalized = true; request->send(); } diff --git a/core/request.cpp b/core/request.cpp index 2a356b2..56129f0 100644 --- a/core/request.cpp +++ b/core/request.cpp @@ -1,5 +1,14 @@ #include "request.h" +void Request::next_stage() { + if (current_middleware_index == (*middleware_stack).size()) { + handler_func(this); + return; + } + + (*middleware_stack)[current_middleware_index++](this); +} + void Request::send() { if (http_parser->isKeepAlive()) { response->addHeadValue("Connection", "Keep-Alive"); @@ -23,7 +32,8 @@ void Request::send() { void Request::reset() { http_parser = nullptr; session = nullptr; - finalized = false; + current_middleware_index = 0; + middleware_stack = nullptr; if (response) delete response; diff --git a/core/request.h b/core/request.h index 59d9691..2d46591 100644 --- a/core/request.h +++ b/core/request.h @@ -16,8 +16,12 @@ public: const HTTPParser *http_parser; const HttpSession::Ptr *session; HttpResponse *response; - bool finalized; + uint32_t current_middleware_index; + std::function handler_func; + std::vector > *middleware_stack; + + void next_stage(); void send(); void reset(); diff --git a/rdn_application.cpp b/rdn_application.cpp index fc48b27..86934c2 100644 --- a/rdn_application.cpp +++ b/rdn_application.cpp @@ -24,7 +24,12 @@ void RDNApplication::index(Request *request) { } void RDNApplication::session_middleware_func(Request *request) { - //std::cout << "test: session_middleware_func called" << std::endl; + std::cout << "test: session_middleware_func called" << std::endl; + + //if fail + //request->send(); in middleware + + request->next_stage(); } void RDNApplication::setup_routes() { @@ -38,7 +43,7 @@ void RDNApplication::setup_routes() { void RDNApplication::setup_middleware() { Application::setup_middleware(); - //middlewares.push_back(RDNApplication::session_middleware_func); + middlewares.push_back(RDNApplication::session_middleware_func); } RDNApplication::RDNApplication() : Application() {