Fix DRequest. File support is still todo.

This commit is contained in:
Relintai 2021-07-06 22:06:18 +02:00
parent 92ce00a88c
commit 4c891a373e
5 changed files with 19 additions and 121 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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();

View File

@ -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<void(Object *, Request *)> 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("<p>Hello, world!</p>");
//resp->setExpiredTime(0);
//callback(resp);
//request->response->setBody("<p>Hello, world!</p>");
//request->response->setExpiredTime(0);
//request->callback(request->response);
request->pool();
}
void DWebApplication::send_error(int error_code, Request *request) {
std::function<void(int, Request *)> 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<std::mutex> lock(_update_registered_requests_mutex);
_update_registered_requests.push_back(request);
}
void DWebApplication::unregister_request_update(Request *request) {
std::lock_guard<std::mutex> 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<std::pair<std::string, std::string> > &sslConfCmds) {
assert(!_running);

View File

@ -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<Request *> _update_registered_requests;
// We use a uuid string as session id;
// set sessionTimeout_=0 to make location session valid forever based on
// cookies;