Added a new WebServerRequestScriptable class, so different web server request implementations can be created using scripts when needed.

This commit is contained in:
Relintai 2023-01-03 21:27:36 +01:00
parent 7244b869a5
commit 238bc63087
5 changed files with 372 additions and 0 deletions

View File

@ -21,6 +21,7 @@ sources = [
"http/web_server_cookie.cpp", "http/web_server_cookie.cpp",
"http/web_server_middleware.cpp", "http/web_server_middleware.cpp",
"http/web_server_request.cpp", "http/web_server_request.cpp",
"http/web_server_request_scriptable.cpp",
"html/html_builder_bind.cpp", "html/html_builder_bind.cpp",
"html/html_builder.cpp", "html/html_builder.cpp",

View File

@ -26,6 +26,7 @@ def get_doc_classes():
"WebServerCookie", "WebServerCookie",
"WebServerMiddleware", "WebServerMiddleware",
"WebServerRequest", "WebServerRequest",
"WebServerRequestScriptable",
#"HTTPParser", #"HTTPParser",
#"HTTPWriter", #"HTTPWriter",

View File

@ -0,0 +1,274 @@
#include "web_server_request_scriptable.h"
#include "web_server.h"
#include "web_node.h"
String WebServerRequestScriptable::get_cookie(const String &key) {
return call("_get_cookie", key);
}
HTTPServerEnums::HTTPMethod WebServerRequestScriptable::get_method() const {
int m = const_cast<WebServerRequestScriptable *>(this)->call("_get_method");
return static_cast<HTTPServerEnums::HTTPMethod>(m);
}
void WebServerRequestScriptable::parse_files() {
call("_parse_files");
}
int WebServerRequestScriptable::get_file_count() const {
return const_cast<WebServerRequestScriptable *>(this)->call("_get_file_count");
}
String WebServerRequestScriptable::get_file_file_name(const int index) const {
return const_cast<WebServerRequestScriptable *>(this)->call("_get_file_file_name", index);
}
String WebServerRequestScriptable::get_file_key(const int index) const {
return const_cast<WebServerRequestScriptable *>(this)->call("_get_file_key", index);
}
int WebServerRequestScriptable::get_file_length(const int index) const {
return const_cast<WebServerRequestScriptable *>(this)->call("_get_file_length", index);
}
PoolByteArray WebServerRequestScriptable::get_file_data(const int index) const {
return const_cast<WebServerRequestScriptable *>(this)->call("_get_file_data", index);
}
String WebServerRequestScriptable::get_file_data_str(const int index) const {
return const_cast<WebServerRequestScriptable *>(this)->call("_get_file_data_str", index);
}
String WebServerRequestScriptable::get_parameter(const String &key) const {
return const_cast<WebServerRequestScriptable *>(this)->call("_get_parameter", key);
}
String WebServerRequestScriptable::get_post_parameter(const String &key) const {
return const_cast<WebServerRequestScriptable *>(this)->call("_get_post_parameter", key);
}
String WebServerRequestScriptable::get_get_parameter(const String &key) const {
return const_cast<WebServerRequestScriptable *>(this)->call("_get_get_parameter", key);
}
void WebServerRequestScriptable::send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code) {
call("_send_redirect", location, status_code);
}
void WebServerRequestScriptable::compile_body() {
call("_compile_body");
}
void WebServerRequestScriptable::compile_and_send_body() {
call("_compile_and_send_body");
}
void WebServerRequestScriptable::send() {
call("_send");
}
void WebServerRequestScriptable::send_file(const String &p_file_path) {
call("_send_file", p_file_path);
}
void WebServerRequestScriptable::send_error(int error_code) {
call("_send_error", error_code);
}
String WebServerRequestScriptable::parser_get_path() {
return call("_parser_get_path");
}
String WebServerRequestScriptable::get_path_full() const {
return const_cast<WebServerRequestScriptable *>(this)->call("_get_path_full");
}
String WebServerRequestScriptable::get_host() const {
return const_cast<WebServerRequestScriptable *>(this)->call("_get_host");
}
void WebServerRequestScriptable::update() {
call("_update");
}
// script virtuals
String WebServerRequestScriptable::_get_cookie(const String &key) {
return "";
}
HTTPServerEnums::HTTPMethod WebServerRequestScriptable::_get_method() const {
return HTTPServerEnums::HTTP_METHOD_GET;
}
void WebServerRequestScriptable::_parse_files() {
}
int WebServerRequestScriptable::_get_file_count() const {
return 0;
}
String WebServerRequestScriptable::_get_file_file_name(const int index) const {
return "";
}
String WebServerRequestScriptable::_get_file_key(const int index) const {
return "";
}
int WebServerRequestScriptable::_get_file_length(const int index) const {
return 0;
}
PoolByteArray WebServerRequestScriptable::_get_file_data(const int index) const {
return PoolByteArray();
}
String WebServerRequestScriptable::_get_file_data_str(const int index) const {
return "";
}
String WebServerRequestScriptable::_get_parameter(const String &key) const {
return "";
}
String WebServerRequestScriptable::_get_post_parameter(const String &key) const {
return "";
}
String WebServerRequestScriptable::_get_get_parameter(const String &key) const {
return "";
}
void WebServerRequestScriptable::_send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code) {
}
void WebServerRequestScriptable::_compile_body() {
//compiled_body.ensure_capacity(body.size() + head.size() + 15 + 13 + 14 + 15 + 1);
// 15
compiled_body += "<!DOCTYPE html>";
// 13
compiled_body += "<html>"
"<head>";
compiled_body += head;
// 14
compiled_body += "</head>"
"<body>";
compiled_body += body;
compiled_body += footer;
// 15
compiled_body += "</body>"
"</html>";
// response->setBody(compiled_body);
}
void WebServerRequestScriptable::_compile_and_send_body() {
compile_body();
send();
}
void WebServerRequestScriptable::_send() {
// if (connection_closed) {
// WebServerRequestScriptablePool::return_request(this);
// return;
// }
// WebServerRequestScriptablePool::return_request(this);
}
void WebServerRequestScriptable::_send_file(const String &p_file_path) {
// WebServerRequestScriptablePool::return_request(this);
}
void WebServerRequestScriptable::_send_error(int error_code) {
}
String WebServerRequestScriptable::_parser_get_path() {
return "";
}
String WebServerRequestScriptable::_get_path_full() const {
return _full_path;
}
String WebServerRequestScriptable::_get_host() const {
return "";
}
void WebServerRequestScriptable::_update() {
}
WebServerRequestScriptable::WebServerRequestScriptable() {
}
WebServerRequestScriptable::~WebServerRequestScriptable() {
}
void WebServerRequestScriptable::_set_server_bind(Node *v) {
_set_server(Object::cast_to<WebServer>(v));
}
void WebServerRequestScriptable::_set_web_root_bind(Node *v) {
_set_web_root(Object::cast_to<WebNode>(v));
}
void WebServerRequestScriptable::_bind_methods() {
BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_cookie", PropertyInfo(Variant::STRING, "key")));
BIND_VMETHOD(MethodInfo(Variant::INT, "_get_method"));
BIND_VMETHOD(MethodInfo("_parse_files"));
BIND_VMETHOD(MethodInfo(Variant::INT, "_get_file_count"));
BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_file_file_name", PropertyInfo(Variant::INT, "index")));
BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_file_key", PropertyInfo(Variant::INT, "index")));
BIND_VMETHOD(MethodInfo(Variant::INT, "_get_file_length", PropertyInfo(Variant::INT, "index")));
BIND_VMETHOD(MethodInfo(Variant::POOL_BYTE_ARRAY, "_get_file_data", PropertyInfo(Variant::INT, "index")));
BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_file_data_str", PropertyInfo(Variant::INT, "index")));
BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_parameter", PropertyInfo(Variant::STRING, "key")));
BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_post_parameter", PropertyInfo(Variant::STRING, "key")));
BIND_VMETHOD(MethodInfo(Variant::STRING, "_get_get_parameter", PropertyInfo(Variant::STRING, "key")));
BIND_VMETHOD(MethodInfo("_send_redirect", PropertyInfo(Variant::STRING, "location"), PropertyInfo(Variant::INT, "status_code")));
BIND_VMETHOD(MethodInfo("_compile_body"));
BIND_VMETHOD(MethodInfo("_compile_and_send_body"));
BIND_VMETHOD(MethodInfo("_send"));
BIND_VMETHOD(MethodInfo("_send_file", PropertyInfo(Variant::STRING, "p_file_path")));
BIND_VMETHOD(MethodInfo("_send_error", PropertyInfo(Variant::INT, "error_code")));
BIND_VMETHOD(MethodInfo("_parser_get_path"));
BIND_VMETHOD(MethodInfo("_get_host"));
BIND_VMETHOD(MethodInfo("_get_path_full"));
BIND_VMETHOD(MethodInfo("_update"));
ClassDB::bind_method(D_METHOD("_get_cookie", "key"), &WebServerRequestScriptable::_get_cookie);
ClassDB::bind_method(D_METHOD("_get_method"), &WebServerRequestScriptable::_get_method);
ClassDB::bind_method(D_METHOD("_parse_files"), &WebServerRequestScriptable::_parse_files);
ClassDB::bind_method(D_METHOD("_get_file_count"), &WebServerRequestScriptable::_get_file_count);
ClassDB::bind_method(D_METHOD("_get_file_file_name", "index"), &WebServerRequestScriptable::_get_file_file_name);
ClassDB::bind_method(D_METHOD("_get_file_key", "index"), &WebServerRequestScriptable::_get_file_key);
ClassDB::bind_method(D_METHOD("_get_file_length", "index"), &WebServerRequestScriptable::_get_file_length);
ClassDB::bind_method(D_METHOD("_get_file_data", "index"), &WebServerRequestScriptable::_get_file_data);
ClassDB::bind_method(D_METHOD("_get_file_data_str", "index"), &WebServerRequestScriptable::_get_file_data_str);
ClassDB::bind_method(D_METHOD("_get_parameter", "key"), &WebServerRequestScriptable::_get_parameter);
ClassDB::bind_method(D_METHOD("_get_post_parameter", "key"), &WebServerRequestScriptable::_get_post_parameter);
ClassDB::bind_method(D_METHOD("_get_get_parameter", "key"), &WebServerRequestScriptable::_get_get_parameter);
ClassDB::bind_method(D_METHOD("_send_redirect", "location", "status_code"), &WebServerRequestScriptable::_send_redirect);
ClassDB::bind_method(D_METHOD("_compile_body"), &WebServerRequestScriptable::_compile_body);
ClassDB::bind_method(D_METHOD("_compile_and_send_body"), &WebServerRequestScriptable::_compile_and_send_body);
ClassDB::bind_method(D_METHOD("_send"), &WebServerRequestScriptable::_send);
ClassDB::bind_method(D_METHOD("_send_file", "p_file_path"), &WebServerRequestScriptable::_send_file);
ClassDB::bind_method(D_METHOD("_send_error", "error_code"), &WebServerRequestScriptable::_send_error);
ClassDB::bind_method(D_METHOD("_parser_get_path"), &WebServerRequestScriptable::_parser_get_path);
ClassDB::bind_method(D_METHOD("_get_host"), &WebServerRequestScriptable::_get_host);
ClassDB::bind_method(D_METHOD("_get_path_full"), &WebServerRequestScriptable::_get_path_full);
ClassDB::bind_method(D_METHOD("_update"), &WebServerRequestScriptable::_update);
ClassDB::bind_method(D_METHOD("_set_server"), &WebServerRequestScriptable::_set_server_bind);
ClassDB::bind_method(D_METHOD("_set_web_root"), &WebServerRequestScriptable::_set_web_root_bind);
}

View File

@ -0,0 +1,94 @@
#ifndef WEB_SERVER_REQUEST_SCRIPTABLE_H
#define WEB_SERVER_REQUEST_SCRIPTABLE_H
#include "core/containers/vector.h"
#include "core/string/ustring.h"
#include "core/variant/dictionary.h"
#include "core/object/object.h"
#include "core/object/reference.h"
#include "http_server_enums.h"
#include "web_server_request.h"
class WebServer;
class WebServerCookie;
class HTTPSession;
class WebPermission;
class WebNode;
class WebServerRequestScriptable : public WebServerRequest {
GDCLASS(WebServerRequestScriptable, WebServerRequest);
public:
virtual String get_cookie(const String &key);
virtual HTTPServerEnums::HTTPMethod get_method() const;
virtual void parse_files();
virtual int get_file_count() const;
virtual String get_file_file_name(const int index) const;
virtual String get_file_key(const int index) const;
virtual int get_file_length(const int index) const;
virtual PoolByteArray get_file_data(const int index) const;
virtual String get_file_data_str(const int index) const;
virtual String get_parameter(const String &key) const;
virtual String get_post_parameter(const String &key) const;
virtual String get_get_parameter(const String &key) const;
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 String parser_get_path();
virtual String get_host() const;
virtual String get_path_full() const;
virtual void update();
// script virtuals
String _get_cookie(const String &key);
HTTPServerEnums::HTTPMethod _get_method() const;
void _parse_files();
int _get_file_count() const;
String _get_file_file_name(const int index) const;
String _get_file_key(const int index) const;
int _get_file_length(const int index) const;
PoolByteArray _get_file_data(const int index) const;
String _get_file_data_str(const int index) const;
String _get_parameter(const String &key) const;
String _get_post_parameter(const String &key) const;
String _get_get_parameter(const String &key) const;
void _send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code);
void _compile_body();
void _compile_and_send_body();
void _send();
void _send_file(const String &p_file_path);
void _send_error(int error_code);
String _parser_get_path();
String _get_host() const;
String _get_path_full() const;
void _update();
WebServerRequestScriptable();
~WebServerRequestScriptable();
void _set_server_bind(Node *v);
void _set_web_root_bind(Node *v);
protected:
static void _bind_methods();
};
#endif

View File

@ -44,6 +44,7 @@ SOFTWARE.
#include "http/web_server_cookie.h" #include "http/web_server_cookie.h"
#include "http/web_server_middleware.h" #include "http/web_server_middleware.h"
#include "http/web_server_request.h" #include "http/web_server_request.h"
#include "http/web_server_request_scriptable.h"
#include "nodes/static_pages/static_web_page.h" #include "nodes/static_pages/static_web_page.h"
#include "nodes/static_pages/static_web_page_file.h" #include "nodes/static_pages/static_web_page_file.h"
@ -118,6 +119,7 @@ void register_web_types() {
ClassDB::register_class<WebServerCookie>(); ClassDB::register_class<WebServerCookie>();
ClassDB::register_class<WebServerMiddleware>(); ClassDB::register_class<WebServerMiddleware>();
ClassDB::register_class<WebServerRequest>(); ClassDB::register_class<WebServerRequest>();
ClassDB::register_class<WebServerRequestScriptable>();
ClassDB::register_class<StaticWebPage>(); ClassDB::register_class<StaticWebPage>();
ClassDB::register_class<StaticWebPageFile>(); ClassDB::register_class<StaticWebPageFile>();