From 413e9efbc83639c1bb992358aed0af4a8bc47715 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 8 Jan 2022 11:42:57 +0100 Subject: [PATCH] Implement setting http status codes. --- core/http/request.cpp | 44 ++++++++++++++++++++++---------------- core/http/request.h | 4 ++++ modules/drogon/request.cpp | 1 + 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/core/http/request.cpp b/core/http/request.cpp index 403a226..0853264 100644 --- a/core/http/request.cpp +++ b/core/http/request.cpp @@ -5,8 +5,8 @@ #include "http_session.h" -#include "session_manager.h" #include "core/http/web_root.h" +#include "session_manager.h" HTTPSession *Request::get_or_create_session() { if (session) { @@ -38,33 +38,40 @@ const String Request::get_parameter(const String &key) const { return str; } +HTTPStatusCode Request::get_status_code() const { + return _status_code; +} +void Request::set_status_code(const HTTPStatusCode status_code) { + _status_code = status_code; +} + void Request::send_redirect(const String &location, const HTTPStatusCode status_code) { } void Request::compile_body() { compiled_body.ensure_capacity(body.size() + head.size() + 15 + 13 + 14 + 15 + 1); - //15 + // 15 compiled_body += ""; - //13 + // 13 compiled_body += "" ""; compiled_body += head; - //14 + // 14 compiled_body += "" ""; compiled_body += body; compiled_body += footer; - //15 + // 15 compiled_body += "" ""; - //response->setBody(compiled_body); + // response->setBody(compiled_body); } void Request::compile_and_send_body() { @@ -73,9 +80,9 @@ void Request::compile_and_send_body() { } void Request::next_stage() { - //if (middleware_stack == nullptr) { + // 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); @@ -88,16 +95,16 @@ void Request::next_stage() { } void Request::send() { - //if (connection_closed) { + // if (connection_closed) { // RequestPool::return_request(this); // return; - //} + // } - //RequestPool::return_request(this); + // RequestPool::return_request(this); } void Request::send_file(const String &p_file_path) { - //RequestPool::return_request(this); + // RequestPool::return_request(this); } void Request::send_error(int error_code) { @@ -115,6 +122,7 @@ void Request::reset() { current_file_progress = 0; connection_closed = false; _full_path = ""; + _status_code = HTTP_STATUS_CODE_200_OK; head.clear(); body.clear(); @@ -171,7 +179,7 @@ const String &Request::get_path_segment(const uint32_t i) const { const String &Request::get_current_path_segment() const { if (_path_stack_pointer >= _path_stack.size()) { - //for convenience + // for convenience static const String e_str = ""; return e_str; } @@ -183,7 +191,7 @@ const String &Request::get_next_path_segment() const { int pst = _path_stack_pointer + 1; if (pst >= _path_stack.size()) { - //for convenience + // for convenience static const String e_str = ""; return e_str; } @@ -292,10 +300,10 @@ void Request::pool() { } Request::Request() { - //This value will need benchmarks, 2 MB seems to be just as fast for me as 4 MB, but 1MB is slower - //It is a tradeoff on server memory though, as every active download will consume this amount of memory - //where the file is bigger than this number - file_chunk_size = 1 << 21; //2MB + // This value will need benchmarks, 2 MB seems to be just as fast for me as 4 MB, but 1MB is slower + // It is a tradeoff on server memory though, as every active download will consume this amount of memory + // where the file is bigger than this number + file_chunk_size = 1 << 21; // 2MB } Request::~Request() { diff --git a/core/http/request.h b/core/http/request.h index 0942bda..6edae9f 100644 --- a/core/http/request.h +++ b/core/http/request.h @@ -54,6 +54,9 @@ public: virtual const String get_parameter(const String &key) const; + HTTPStatusCode get_status_code() const; + void set_status_code(const HTTPStatusCode status_code); + virtual void send_redirect(const String &location, const HTTPStatusCode status_code = HTTP_STATUS_CODE_302_FOUND); virtual void compile_body(); virtual void compile_and_send_body(); @@ -93,6 +96,7 @@ public: virtual ~Request(); protected: + HTTPStatusCode _status_code; String _full_path; Vector _path_stack; uint32_t _path_stack_pointer; diff --git a/modules/drogon/request.cpp b/modules/drogon/request.cpp index ce66c54..75815ea 100644 --- a/modules/drogon/request.cpp +++ b/modules/drogon/request.cpp @@ -47,6 +47,7 @@ void DRequest::send() { _response_additional_setup(response); + response->setStatusCode(static_cast(static_cast(_status_code))); response->setBody(compiled_body); response->setExpiredTime(0);