diff --git a/core/application.cpp b/core/application.cpp index 254a943..ed92d18 100644 --- a/core/application.cpp +++ b/core/application.cpp @@ -25,12 +25,14 @@ void Application::default_fallback_error_handler(int error_code, Request *reques std::string body = "Internal server error! :("; request->response->setBody(body); request->finalized = true; + request->send(); } void Application::default_404_error_handler(int error_code, Request *request) { std::string body = "404 :("; request->response->setBody(body); request->finalized = true; + request->send(); } void Application::handle_request(Request *request) { @@ -72,8 +74,10 @@ void Application::handle_request(Request *request) { for (uint32_t i = 0; i < middlewares.size(); ++i) { middlewares[i](request); - if (request->finalized) + if (request->finalized) { + request->send(); return; + } } func(request); @@ -116,6 +120,7 @@ void Application::send_file(const std::string &path, Request *request) { request->response->setBody(body); request->finalized = true; + request->send(); } Application::Application() { diff --git a/core/http_server.cpp b/core/http_server.cpp index 60d13c7..4e41b9e 100644 --- a/core/http_server.cpp +++ b/core/http_server.cpp @@ -10,8 +10,6 @@ void HTTPServer::http_callback_handler(Request *request) { } void HTTPServer::httpEnterCallbackDefault(const HTTPParser &httpParser, const HttpSession::Ptr &session) { - //(void)httpParser; needed? - Request *request = RequestPool::get_request(); request->http_parser = &httpParser; @@ -22,22 +20,6 @@ void HTTPServer::httpEnterCallbackDefault(const HTTPParser &httpParser, const Ht #endif http_callback_handler(request); - - if (httpParser.isKeepAlive()) { - request->response->addHeadValue("Connection", "Keep-Alive"); - - std::string result = request->response->getResult(); - - session->send(result.c_str(), result.size()); - } else { - request->response->addHeadValue("Connection", "Close"); - - std::string result = request->response->getResult(); - - session->send(result.c_str(), result.size(), [session]() { session->postShutdown(); }); - } - - RequestPool::return_request(request); } void HTTPServer::wsEnterCallbackDefault(const HttpSession::Ptr &httpSession, WebSocketFormat::WebSocketFrameType opcode, const std::string &payload) { @@ -66,6 +48,7 @@ void HTTPServer::initialize() { int p_port = port; + //! if (listenBuilder) delete listenBuilder; @@ -95,6 +78,7 @@ void HTTPServer::initialize() { void HTTPServer::main_loop() { while (true) { std::this_thread::sleep_for(std::chrono::seconds(1)); + if (brynet::base::app_kbhit()) { break; } @@ -104,6 +88,7 @@ void HTTPServer::main_loop() { HTTPServer::HTTPServer() { port = 80; threads = 4; + listenBuilder = nullptr; } HTTPServer::~HTTPServer() { diff --git a/core/request.cpp b/core/request.cpp index a0b7255..2a356b2 100644 --- a/core/request.cpp +++ b/core/request.cpp @@ -1,9 +1,29 @@ #include "request.h" +void Request::send() { + if (http_parser->isKeepAlive()) { + response->addHeadValue("Connection", "Keep-Alive"); + + std::string result = response->getResult(); + + (*session)->send(result.c_str(), result.size()); + } else { + response->addHeadValue("Connection", "Close"); + + std::string result = response->getResult(); + + const HttpSession::Ptr lsession = (*session); + + (*session)->send(result.c_str(), result.size(), [lsession]() { lsession->postShutdown(); }); + } + + RequestPool::return_request(this); +} + void Request::reset() { http_parser = nullptr; session = nullptr; - finalized = false; + finalized = false; if (response) delete response; @@ -12,7 +32,7 @@ void Request::reset() { } Request::Request() { - response = nullptr; + response = nullptr; reset(); } @@ -43,11 +63,11 @@ Request *RequestPool::get_request() { } void RequestPool::return_request(Request *request) { - request->reset(); + request->reset(); - _mutex.lock(); + _mutex.lock(); _requests.push_back(request); - _mutex.unlock(); + _mutex.unlock(); } RequestPool::RequestPool() { diff --git a/core/request.h b/core/request.h index e5aa63a..59d9691 100644 --- a/core/request.h +++ b/core/request.h @@ -1,11 +1,11 @@ #ifndef REQUEST_H #define REQUEST_H -#include #include +#include -#include #include +#include using namespace brynet; using namespace brynet::net; @@ -14,27 +14,28 @@ using namespace brynet::net::http; class Request { public: const HTTPParser *http_parser; - const HttpSession::Ptr *session; + const HttpSession::Ptr *session; HttpResponse *response; - bool finalized; + bool finalized; - void reset(); + void send(); + void reset(); - Request(); - ~Request(); + Request(); + ~Request(); }; class RequestPool { public: - static Request *get_request(); - static void return_request(Request *request); + static Request *get_request(); + static void return_request(Request *request); - RequestPool(); - ~RequestPool(); + RequestPool(); + ~RequestPool(); protected: - static std::mutex _mutex; - static std::vector _requests; + static std::mutex _mutex; + static std::vector _requests; }; #endif \ No newline at end of file diff --git a/rdn_application.cpp b/rdn_application.cpp index 1fc4c61..fc48b27 100644 --- a/rdn_application.cpp +++ b/rdn_application.cpp @@ -20,6 +20,7 @@ void RDNApplication::index(Request *request) { FileCache::get_singleton()->set_cached_body("index", body); request->response->setBody(body); + request->send(); } void RDNApplication::session_middleware_func(Request *request) {