Now the request handling/middlewares work in a fully async way.

This commit is contained in:
Relintai 2020-11-26 12:02:58 +01:00
parent cc8de9ed7f
commit 40471f0525
4 changed files with 26 additions and 16 deletions

View File

@ -23,13 +23,11 @@ void Application::setup_middleware() {
void Application::default_fallback_error_handler(int error_code, Request *request) { void Application::default_fallback_error_handler(int error_code, Request *request) {
request->response->setBody(default_generic_error_body); request->response->setBody(default_generic_error_body);
request->finalized = true;
request->send(); request->send();
} }
void Application::default_404_error_handler(int error_code, Request *request) { void Application::default_404_error_handler(int error_code, Request *request) {
request->response->setBody(default_error_404_body); request->response->setBody(default_error_404_body);
request->finalized = true;
request->send(); request->send();
} }
@ -69,16 +67,10 @@ void Application::handle_request(Request *request) {
return; return;
} }
for (uint32_t i = 0; i < middlewares.size(); ++i) { request->handler_func = func;
middlewares[i](request); request->middleware_stack = &middlewares;
if (request->finalized) { request->next_stage();
request->send();
return;
}
}
func(request);
} }
void Application::send_error(int error_code, Request *request) { 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? //TODO set mimetype?
request->response->setBody(body); request->response->setBody(body);
request->finalized = true;
request->send(); request->send();
} }

View File

@ -1,5 +1,14 @@
#include "request.h" #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() { void Request::send() {
if (http_parser->isKeepAlive()) { if (http_parser->isKeepAlive()) {
response->addHeadValue("Connection", "Keep-Alive"); response->addHeadValue("Connection", "Keep-Alive");
@ -23,7 +32,8 @@ void Request::send() {
void Request::reset() { void Request::reset() {
http_parser = nullptr; http_parser = nullptr;
session = nullptr; session = nullptr;
finalized = false; current_middleware_index = 0;
middleware_stack = nullptr;
if (response) if (response)
delete response; delete response;

View File

@ -16,8 +16,12 @@ public:
const HTTPParser *http_parser; const HTTPParser *http_parser;
const HttpSession::Ptr *session; const HttpSession::Ptr *session;
HttpResponse *response; HttpResponse *response;
bool finalized;
uint32_t current_middleware_index;
std::function<void(Request *)> handler_func;
std::vector<std::function<void(Request *)> > *middleware_stack;
void next_stage();
void send(); void send();
void reset(); void reset();

View File

@ -24,7 +24,12 @@ void RDNApplication::index(Request *request) {
} }
void RDNApplication::session_middleware_func(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() { void RDNApplication::setup_routes() {
@ -38,7 +43,7 @@ void RDNApplication::setup_routes() {
void RDNApplication::setup_middleware() { void RDNApplication::setup_middleware() {
Application::setup_middleware(); Application::setup_middleware();
//middlewares.push_back(RDNApplication::session_middleware_func); middlewares.push_back(RDNApplication::session_middleware_func);
} }
RDNApplication::RDNApplication() : Application() { RDNApplication::RDNApplication() : Application() {