diff --git a/core/http/request.cpp b/core/http/request.cpp index 5aff92b..19c0d2e 100644 --- a/core/http/request.cpp +++ b/core/http/request.cpp @@ -31,6 +31,10 @@ void Request::compile_and_send_body() { } void Request::next_stage() { + //if (middleware_stack == nullptr) { + // printf("Error Request::next_stage-> middleware_stack == nullptr\n"); + //} + if (current_middleware_index == (*middleware_stack).size()) { handler_instance.handler_func(handler_instance.instance, this); return; diff --git a/modules/drogon/request.cpp b/modules/drogon/request.cpp index 1ff2931..9984de5 100644 --- a/modules/drogon/request.cpp +++ b/modules/drogon/request.cpp @@ -8,7 +8,9 @@ void DRequest::send() { // return; //} - //response->setExpiredTime(0); + response->setBody(compiled_body); + + response->setExpiredTime(0); callback(response); pool(); @@ -42,6 +44,8 @@ void DRequest::send_file(const std::string &p_file_path) { session->send(result.c_str(), result.size(), [this]() { this->_file_chunk_sent(); }); */ + send_error(404); + pool(); } @@ -54,6 +58,10 @@ void DRequest::reset() { //response = new HttpResponse(); } +std::string DRequest::parser_get_path() { + return request->getPath(); +} + void DRequest::update() { if (file_next) { file_next = false; diff --git a/modules/drogon/request.h b/modules/drogon/request.h index 3c9ad69..960e561 100644 --- a/modules/drogon/request.h +++ b/modules/drogon/request.h @@ -24,6 +24,7 @@ public: void send(); void send_file(const std::string &p_file_path); void reset(); + std::string parser_get_path(); void update(); diff --git a/modules/drogon/web_application.cpp b/modules/drogon/web_application.cpp index 99d1139..a392b7f 100644 --- a/modules/drogon/web_application.cpp +++ b/modules/drogon/web_application.cpp @@ -40,111 +40,9 @@ void DWebApplication::setup_middleware() { middlewares.push_back(HandlerInstance([this](Object *instance, Request *request) { this->default_routing_middleware(instance, request); })); } -void DWebApplication::default_routing_middleware(Object *instance, Request *request) { - /* - std::string path = request->http_parser->getPath(); - - if (FileCache::get_singleton()->wwwroot_has_file(path)) { - send_file(path, request); - - return; - } - - DHandlerInstance handler_data; - - //std::function func; - - //if (path == "/") { - if (request->get_path_segment_count() == 0) { - //quick shortcut - handler_data = index_func; - } else { - const std::string main_route = request->get_current_path_segment(); - - handler_data = main_route_map[main_route]; - - request->push_path(); - } - - if (!handler_data.handler_func) { - send_error(404, request); - - return; - } - - request->handler_instance = handler_data; - request->next_stage(); - */ -} - -void DWebApplication::handle_request(Request *request) { - request->middleware_stack = &middlewares; - - //note that middlewares handle the routing -> DWebApplication::default_routing_middleware by default - request->next_stage(); - - //auto resp = HttpResponse::newHttpResponse(); - //resp->setBody("

Hello, world!

"); - //resp->setExpiredTime(0); - //callback(resp); - - //request->response->setBody("

Hello, world!

"); - //request->response->setExpiredTime(0); - //request->callback(request->response); - - request->pool(); -} - -void DWebApplication::send_error(int error_code, Request *request) { - std::function func = error_handler_map[error_code]; - - if (!func) { - default_error_handler_func(error_code, request); - return; - } - - func(error_code, request); -} - -void DWebApplication::send_file(const std::string &path, Request *request) { - std::string fp = FileCache::get_singleton()->wwwroot + path; - - request->send_file(fp); -} - void DWebApplication::migrate() { } -void DWebApplication::register_request_update(Request *request) { - std::lock_guard lock(_update_registered_requests_mutex); - - _update_registered_requests.push_back(request); -} -void DWebApplication::unregister_request_update(Request *request) { - std::lock_guard lock(_update_registered_requests_mutex); - - std::size_t s = _update_registered_requests.size(); - for (std::size_t i = 0; i < s; ++i) { - Request *r = _update_registered_requests[i]; - - if (r == request) { - _update_registered_requests[i] = _update_registered_requests[s - 1]; - - _update_registered_requests.pop_back(); - - return; - } - } -} - -void DWebApplication::update() { - for (std::size_t i = 0; i < _update_registered_requests.size(); ++i) { - Request *r = _update_registered_requests[i]; - - r->update(); - } -} - void DWebApplication::add_listener(const std::string &ip, uint16_t port, bool useSSL, const std::string &certFile, const std::string &keyFile, bool useOldTLS, const std::vector > &sslConfCmds) { assert(!_running); diff --git a/modules/drogon/web_application.h b/modules/drogon/web_application.h index 4cdbae7..85215bc 100644 --- a/modules/drogon/web_application.h +++ b/modules/drogon/web_application.h @@ -29,23 +29,13 @@ using namespace drogon; class Request; -class DWebApplication : WebApplication { +class DWebApplication : public WebApplication { public: - void handle_request(Request *request); - void send_error(int error_code, Request *request); - void send_file(const std::string &path, Request *request); + void load_settings(); + void setup_routes(); + void setup_middleware(); - virtual void load_settings(); - virtual void setup_routes(); - virtual void setup_middleware(); - - void default_routing_middleware(Object *instance, Request *request); - - virtual void migrate(); - - void register_request_update(Request *request); - void unregister_request_update(Request *request); - void update(); + void migrate(); void add_listener(const std::string &ip, uint16_t port, bool useSSL = false, const std::string &certFile = "", const std::string &keyFile = "", bool useOldTLS = false, @@ -164,9 +154,6 @@ public: virtual ~DWebApplication(); protected: - std::mutex _update_registered_requests_mutex; - std::vector _update_registered_requests; - // We use a uuid string as session id; // set sessionTimeout_=0 to make location session valid forever based on // cookies;