Implement setting http status codes.

This commit is contained in:
Relintai 2022-01-08 11:42:57 +01:00
parent e0b76706d9
commit 413e9efbc8
3 changed files with 31 additions and 18 deletions

View File

@ -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 += "<!DOCTYPE html>";
//13
// 13
compiled_body += "<html>"
"<head>";
compiled_body += head;
//14
// 14
compiled_body += "</head>"
"<body>";
compiled_body += body;
compiled_body += footer;
//15
// 15
compiled_body += "</body>"
"</html>";
//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() {

View File

@ -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<String> _path_stack;
uint32_t _path_stack_pointer;

View File

@ -47,6 +47,7 @@ void DRequest::send() {
_response_additional_setup(response);
response->setStatusCode(static_cast<const HttpStatusCode>(static_cast<const int>(_status_code)));
response->setBody(compiled_body);
response->setExpiredTime(0);