WebServerRequest cleanup + bindings.

This commit is contained in:
Relintai 2022-06-26 19:25:57 +02:00
parent 19cd0fe4e2
commit 9ce119a5ab
2 changed files with 269 additions and 86 deletions

View File

@ -1,15 +1,79 @@
#include "web_server_request.h"
#include "web/http/cookie.h"
#include "core/object.h"
#include "web_server.h"
#include "web_server_cookie.h"
#include "http_session.h"
#include "session_manager.h"
#include "web/http/web_root.h"
#include "web_root.h"
#include "web_permission.h"
String WebServerRequest::get_head() {
return head;
}
void WebServerRequest::set_head(const String &val) {
head = val;
}
String WebServerRequest::get_body() {
return body;
}
void WebServerRequest::set_body(const String &val) {
body = val;
}
String WebServerRequest::get_footer() {
return footer;
}
void WebServerRequest::set_footer(const String &val) {
footer = val;
}
String WebServerRequest::get_compiled_body() {
return compiled_body;
}
void WebServerRequest::set_compiled_body(const String &val) {
compiled_body = val;
}
bool WebServerRequest::get_connection_closed() {
return connection_closed;
}
void WebServerRequest::set_connection_closed(const bool &val) {
connection_closed = val;
}
Ref<HTTPSession> WebServerRequest::get_session() {
return session;
}
void WebServerRequest::set_session(const Ref<HTTPSession> &val) {
session = val;
}
Dictionary WebServerRequest::get_data() {
return data;
}
void WebServerRequest::set_data(const Dictionary &val) {
data = val;
}
Ref<WebPermission> WebServerRequest::get_active_permission() {
return active_permission;
}
void WebServerRequest::set_active_permission(const Ref<WebPermission> &val) {
active_permission = val;
}
int WebServerRequest::get_permissions() {
return permissions;
}
void WebServerRequest::set_permissions(const int &val) {
permissions = val;
}
Ref<HTTPSession> WebServerRequest::get_or_create_session() {
if (session.is_valid()) {
return session;
@ -48,11 +112,7 @@ String WebServerRequest::get_csrf_token() {
const Variant &val = session->get_const("csrf_token");
if (val.is_simple_type()) {
return val.to_string();
}
return "";
return val;
}
void WebServerRequest::set_csrf_token(const String &value) {
@ -65,7 +125,7 @@ void WebServerRequest::set_csrf_token(const String &value) {
bool WebServerRequest::validate_csrf_token() {
String param_token = get_parameter("csrf_token");
param_token.trim();
param_token.strip_edges();
if (param_token == "") {
return false;
@ -81,7 +141,7 @@ bool WebServerRequest::validate_csrf_token() {
}
const String WebServerRequest::get_cookie(const String &key) {
static String str(0);
static String str;
return str;
}
@ -91,8 +151,8 @@ void WebServerRequest::add_cookie(const ::WebServerCookie &cookie) {
void WebServerRequest::remove_cookie(const String &key) {
}
HTTPMethod WebServerRequest::get_method() const {
return HTTP_METHOD_GET;
HTTPServerEnums::HTTPMethod WebServerRequest::get_method() const {
return HTTPServerEnums::HTTP_METHOD_GET;
}
void WebServerRequest::parse_files() {
@ -108,22 +168,22 @@ const uint8_t *WebServerRequest::get_file_data(const int index) const {
}
const String WebServerRequest::get_parameter(const String &key) const {
static String str(0);
static String str;
return str;
}
HTTPStatusCode WebServerRequest::get_status_code() const {
HTTPServerEnums::HTTPStatusCode WebServerRequest::get_status_code() const {
return _status_code;
}
void WebServerRequest::set_status_code(const HTTPStatusCode status_code) {
void WebServerRequest::set_status_code(const HTTPServerEnums::HTTPStatusCode status_code) {
_status_code = status_code;
}
void WebServerRequest::send_redirect(const String &location, const HTTPStatusCode status_code) {
void WebServerRequest::send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code) {
}
void WebServerRequest::compile_body() {
compiled_body.ensure_capacity(body.size() + head.size() + 15 + 13 + 14 + 15 + 1);
//compiled_body.ensure_capacity(body.size() + head.size() + 15 + 13 + 14 + 15 + 1);
// 15
compiled_body += "<!DOCTYPE html>";
@ -170,29 +230,6 @@ void WebServerRequest::send_error(int error_code) {
server->get_web_root()->handle_error_send_request(this, error_code);
}
void WebServerRequest::reset() {
session = nullptr;
server = nullptr;
_path_stack.clear();
_path_stack_pointer = 0;
file_size = 0;
current_file_progress = 0;
connection_closed = false;
_full_path = "";
_status_code = HTTP_STATUS_CODE_200_OK;
// Maybe set NONE or only VIEW as default?
permissions = WebPermission::WEB_PERMISSION_ALL;
active_permission.unref();
head.clear();
body.clear();
footer.clear();
compiled_body.clear();
data.clear();
reference_data.clear();
}
String WebServerRequest::parser_get_path() {
return "";
}
@ -203,7 +240,8 @@ void WebServerRequest::setup_url_stack() {
size_t pos = 0;
String st;
while ((pos = path.find('/')) != -1) {
while ((pos = path.find("/")) != -1) {
st = path.substr(0, pos);
if (st.size() != 0) {
@ -231,21 +269,21 @@ String WebServerRequest::get_path(const bool beginning_slash, const bool end_sla
}
if (!end_slash && path.size() > 1) {
path.pop_back();
path.resize(path.size() - 1);
}
return path;
}
const String &WebServerRequest::get_path_full() const {
String WebServerRequest::path_full() const {
return _full_path;
}
const String &WebServerRequest::get_path_segment(const uint32_t i) const {
String WebServerRequest::path_segment(const uint32_t i) const {
return _path_stack[i];
}
const String &WebServerRequest::get_current_path_segment() const {
String WebServerRequest::current_path_segment() const {
if (_path_stack_pointer >= _path_stack.size()) {
// for convenience
static const String e_str = "";
@ -255,7 +293,7 @@ const String &WebServerRequest::get_current_path_segment() const {
return _path_stack[_path_stack_pointer];
}
const String &WebServerRequest::get_next_path_segment() const {
String WebServerRequest::next_path_segment() const {
int pst = _path_stack_pointer + 1;
if (pst >= _path_stack.size()) {
@ -347,13 +385,13 @@ String WebServerRequest::get_url_site() const {
return path;
}
String WebServerRequest::get_url_root_parent(const String &add) const {
String WebServerRequest::get_url_root_parent_add(const String &add) const {
return get_url_root_parent() + add;
}
String WebServerRequest::get_url_root(const String &add) const {
String WebServerRequest::get_url_root_add(const String &add) const {
return get_url_root() + add;
}
String WebServerRequest::get_url_site(const String &add) const {
String WebServerRequest::get_url_site_add(const String &add) const {
return get_url_site() + add;
}
@ -364,15 +402,137 @@ String WebServerRequest::get_host() const {
void WebServerRequest::update() {
}
void WebServerRequest::pool() {
WebServer *WebServerRequest::get_server() {
return server;
}
Node *WebServerRequest::get_server_bind() {
return server;
}
WebServerRequest::WebServerRequest() {
// 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
//session.unref();
//server = nullptr;
//_path_stack.clear();
_path_stack_pointer = 0;
connection_closed = false;
//_full_path = "";
_status_code = HTTPServerEnums::HTTP_STATUS_CODE_200_OK;
// Maybe set NONE or only VIEW as default?
permissions = WebPermission::WEB_PERMISSION_ALL;
//active_permission.unref();
//head.clear();
//body.clear();
//footer.clear();
//compiled_body.clear();
//data.clear();
}
WebServerRequest::~WebServerRequest() {
}
void WebServerRequest::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_head"), &WebServerRequest::head);
ClassDB::bind_method(D_METHOD("set_head", "val"), &WebServerRequest::set_head);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "head"), "set_head", "get_head");
ClassDB::bind_method(D_METHOD("get_body"), &WebServerRequest::body);
ClassDB::bind_method(D_METHOD("set_body", "val"), &WebServerRequest::set_body);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "body"), "set_body", "get_body");
ClassDB::bind_method(D_METHOD("get_footer"), &WebServerRequest::footer);
ClassDB::bind_method(D_METHOD("set_footer", "val"), &WebServerRequest::set_footer);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "footer"), "set_footer", "get_footer");
ClassDB::bind_method(D_METHOD("get_compiled_body"), &WebServerRequest::compiled_body);
ClassDB::bind_method(D_METHOD("set_compiled_body", "val"), &WebServerRequest::set_compiled_body);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "compiled_body"), "set_compiled_body", "get_compiled_body");
ClassDB::bind_method(D_METHOD("get_connection_closed"), &WebServerRequest::connection_closed);
ClassDB::bind_method(D_METHOD("set_connection_closed", "val"), &WebServerRequest::set_connection_closed);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "connection_closed"), "set_connection_closed", "get_connection_closed");
ClassDB::bind_method(D_METHOD("get_session"), &WebServerRequest::session);
ClassDB::bind_method(D_METHOD("set_session", "val"), &WebServerRequest::set_session);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "session", PROPERTY_HINT_RESOURCE_TYPE, "HTTPSession"), "set_session", "get_session");
ClassDB::bind_method(D_METHOD("get_data"), &WebServerRequest::data);
ClassDB::bind_method(D_METHOD("set_data", "val"), &WebServerRequest::set_data);
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data"), "set_data", "get_data");
ClassDB::bind_method(D_METHOD("get_active_permission"), &WebServerRequest::active_permission);
ClassDB::bind_method(D_METHOD("set_active_permission", "val"), &WebServerRequest::set_active_permission);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "active_permission", PROPERTY_HINT_RESOURCE_TYPE, "WebPermission"), "set_active_permission", "get_active_permission");
ClassDB::bind_method(D_METHOD("get_permissions"), &WebServerRequest::get_permissions);
ClassDB::bind_method(D_METHOD("set_permissions", "val"), &WebServerRequest::set_permissions);
ADD_PROPERTY(PropertyInfo(Variant::INT, "permissions"), "set_permissions", "get_permissions");
ClassDB::bind_method(D_METHOD("get_or_create_session"), &WebServerRequest::get_or_create_session);
ClassDB::bind_method(D_METHOD("can_view"), &WebServerRequest::can_view);
ClassDB::bind_method(D_METHOD("can_create"), &WebServerRequest::can_create);
ClassDB::bind_method(D_METHOD("can_edit"), &WebServerRequest::can_edit);
ClassDB::bind_method(D_METHOD("can_delete"), &WebServerRequest::can_delete);
ClassDB::bind_method(D_METHOD("has_csrf_token"), &WebServerRequest::has_csrf_token);
ClassDB::bind_method(D_METHOD("get_csrf_token"), &WebServerRequest::get_csrf_token);
ClassDB::bind_method(D_METHOD("set_csrf_token", "value"), &WebServerRequest::set_csrf_token);
ClassDB::bind_method(D_METHOD("validate_csrf_token"), &WebServerRequest::validate_csrf_token);
ClassDB::bind_method(D_METHOD("get_cookie", "key"), &WebServerRequest::get_cookie);
ClassDB::bind_method(D_METHOD("add_cookie", "cookie"), &WebServerRequest::add_cookie);
ClassDB::bind_method(D_METHOD("remove_cookie", "key"), &WebServerRequest::remove_cookie);
ClassDB::bind_method(D_METHOD("get_method"), &WebServerRequest::get_method);
//virtual const uint8_t *get_file_data(const int index) const;
ClassDB::bind_method(D_METHOD("parse_files"), &WebServerRequest::parse_files);
ClassDB::bind_method(D_METHOD("get_file_count"), &WebServerRequest::get_file_count);
ClassDB::bind_method(D_METHOD("get_file_length", "index"), &WebServerRequest::get_file_length);
//ClassDB::bind_method(D_METHOD(""), &WebServerRequest::);
ClassDB::bind_method(D_METHOD("get_parameter", "key"), &WebServerRequest::get_parameter);
ClassDB::bind_method(D_METHOD("get_status_code"), &WebServerRequest::get_status_code);
ClassDB::bind_method(D_METHOD("set_status_code", "val"), &WebServerRequest::set_status_code);
ADD_PROPERTY(PropertyInfo(Variant::INT, "status_code"), "set_status_code", "get_status_code");
ClassDB::bind_method(D_METHOD("send_redirect", "location", "status_code "), &WebServerRequest::send_redirect);
ClassDB::bind_method(D_METHOD("compile_body"), &WebServerRequest::compile_body);
ClassDB::bind_method(D_METHOD("compile_and_send_body"), &WebServerRequest::compile_and_send_body);
ClassDB::bind_method(D_METHOD("send"), &WebServerRequest::send);
ClassDB::bind_method(D_METHOD("send_file", "file_path"), &WebServerRequest::send_file);
ClassDB::bind_method(D_METHOD("send_error", "error_code"), &WebServerRequest::send_error);
ClassDB::bind_method(D_METHOD("parser_get_path"), &WebServerRequest::parser_get_path);
ClassDB::bind_method(D_METHOD("get_host"), &WebServerRequest::get_host);
ClassDB::bind_method(D_METHOD("setup_url_stack"), &WebServerRequest::setup_url_stack);
ClassDB::bind_method(D_METHOD("get_path", "beginning_slash", "end_slash "), &WebServerRequest::get_path, false, true);
ClassDB::bind_method(D_METHOD("get_path_full"), &WebServerRequest::get_path_full);
ClassDB::bind_method(D_METHOD("get_path_segment", "i"), &WebServerRequest::get_path_segment);
ClassDB::bind_method(D_METHOD("get_current_path_segment"), &WebServerRequest::get_current_path_segment);
ClassDB::bind_method(D_METHOD("get_next_path_segment"), &WebServerRequest::get_next_path_segment);
ClassDB::bind_method(D_METHOD("get_path_segment_count"), &WebServerRequest::get_path_segment_count);
ClassDB::bind_method(D_METHOD("get_current_segment_index"), &WebServerRequest::get_current_segment_index);
ClassDB::bind_method(D_METHOD("get_remaining_segment_count"), &WebServerRequest::get_remaining_segment_count);
ClassDB::bind_method(D_METHOD("pop_path"), &WebServerRequest::pop_path);
ClassDB::bind_method(D_METHOD("push_path"), &WebServerRequest::push_path);
ClassDB::bind_method(D_METHOD("get_url_root_parent", "parent"), &WebServerRequest::get_url_root_parent, 1);
ClassDB::bind_method(D_METHOD("get_url_root"), &WebServerRequest::get_url_root);
ClassDB::bind_method(D_METHOD("get_url_root_current"), &WebServerRequest::get_url_root_current);
ClassDB::bind_method(D_METHOD("get_url_site"), &WebServerRequest::get_url_site);
ClassDB::bind_method(D_METHOD("get_url_root_parent_add", "add"), &WebServerRequest::get_url_root_parent_add);
ClassDB::bind_method(D_METHOD("get_url_root_add", "add"), &WebServerRequest::get_url_root_add);
ClassDB::bind_method(D_METHOD("get_url_site_add", "add"), &WebServerRequest::get_url_site_add);
//virtual void update();
//TODO
//WebServer *get_server();
//Node *get_server_bind();
}

View File

@ -1,7 +1,7 @@
#ifndef WEB_SERVER_REQUEST_H
#define WEB_SERVER_REQUEST_H
#include "core/hash_map.h"
#include "core/dictionary.h"
#include "core/ustring.h"
#include "core/vector.h"
@ -19,30 +19,35 @@ class WebServerRequest : public Reference {
GDCLASS(WebServerRequest, Reference);
public:
WebServer *server;
String get_head();
void set_head(const String &val);
String head;
String body;
String footer;
String compiled_body;
String get_body();
void set_body(const String &val);
String file_path;
long file_size;
long current_file_progress;
long file_chunk_size;
bool file_next;
String get_footer();
void set_footer(const String &val);
bool connection_closed;
String get_compiled_body();
void set_compiled_body(const String &val);
Ref<HTTPSession> session;
HashMap<String, Object *> data;
HashMap<String, Ref<Reference>> reference_data;
bool get_connection_closed();
void set_connection_closed(const bool &val);
Ref<HTTPSession> get_session();
void set_session(const Ref<HTTPSession> &val);
Dictionary get_data();
void set_data(const Dictionary &val);
Ref<WebPermission> get_active_permission();
void set_active_permission(const Ref<WebPermission> &val);
int get_permissions();
void set_permissions(const int &val);
Ref<HTTPSession> get_or_create_session();
Ref<WebPermission> active_permission;
int permissions;
bool can_view() const;
bool can_create() const;
bool can_edit() const;
@ -57,7 +62,7 @@ public:
virtual void add_cookie(const ::WebServerCookie &cookie);
virtual void remove_cookie(const String &key);
virtual HTTPMethod get_method() const;
virtual HTTPServerEnums::HTTPMethod get_method() const;
virtual void parse_files();
virtual int get_file_count() const;
@ -66,25 +71,24 @@ public:
virtual const String get_parameter(const String &key) const;
HTTPStatusCode get_status_code() const;
void set_status_code(const HTTPStatusCode status_code);
HTTPServerEnums::HTTPStatusCode get_status_code() const;
void set_status_code(const HTTPServerEnums::HTTPStatusCode status_code);
virtual void send_redirect(const String &location, const HTTPStatusCode status_code = HTTP_STATUS_CODE_302_FOUND);
virtual void send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code = HTTPServerEnums::HTTP_STATUS_CODE_302_FOUND);
virtual void compile_body();
virtual void compile_and_send_body();
virtual void send();
virtual void send_file(const String &p_file_path);
virtual void send_error(int error_code);
virtual void reset();
virtual String parser_get_path();
virtual String get_host() const;
void setup_url_stack();
String get_path(const bool beginning_slash = false, const bool end_slash = true) const;
virtual const String &get_path_full() const;
const String &get_path_segment(const uint32_t i) const;
const String &get_current_path_segment() const;
const String &get_next_path_segment() const;
virtual String get_path_full() const;
String get_path_segment(const uint32_t i) const;
String get_current_path_segment() const;
String get_next_path_segment() const;
uint32_t get_path_segment_count() const;
uint32_t get_current_segment_index() const;
uint32_t get_remaining_segment_count() const;
@ -96,18 +100,37 @@ public:
String get_url_root_current() const;
String get_url_site() const;
String get_url_root_parent(const String &add) const;
String get_url_root(const String &add) const;
String get_url_site(const String &add) const;
String get_url_root_parent_add(const String &add) const;
String get_url_root_add(const String &add) const;
String get_url_site_add(const String &add) const;
virtual void update();
virtual void pool();
WebServer *get_server();
Node *get_server_bind();
WebServerRequest();
virtual ~WebServerRequest();
~WebServerRequest();
WebServer *server;
String head;
String body;
String footer;
String compiled_body;
bool connection_closed;
Ref<HTTPSession> session;
Dictionary data;
Ref<WebPermission> active_permission;
int permissions;
protected:
HTTPStatusCode _status_code;
static void _bind_methods();
HTTPServerEnums::HTTPStatusCode _status_code;
String _full_path;
Vector<String> _path_stack;
uint32_t _path_stack_pointer;