From a781853b2b02aef5b5183b61c95bcc6dcd431ab1 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 22 Dec 2023 13:14:17 +0100 Subject: [PATCH] Added a way to get all available marameter keys to WebServerRequest. --- .../simple_web_server_request.cpp | 35 ++++++++++++++++++ .../simple_web_server_request.h | 5 +++ modules/web/http/web_server_request.cpp | 18 ++++++++++ modules/web/http/web_server_request.h | 5 +++ .../http/web_server_request_scriptable.cpp | 36 +++++++++++++++++++ .../web/http/web_server_request_scriptable.h | 10 ++++++ 6 files changed, 109 insertions(+) diff --git a/modules/http_server_simple/simple_web_server_request.cpp b/modules/http_server_simple/simple_web_server_request.cpp index 46f190534..8f3e3511b 100644 --- a/modules/http_server_simple/simple_web_server_request.cpp +++ b/modules/http_server_simple/simple_web_server_request.cpp @@ -135,6 +135,41 @@ void SimpleWebServerRequest::set_header_parameter(const String &key, const Strin _header_parameters[key] = value; } +PoolStringArray SimpleWebServerRequest::get_header_parameter_keys() const { + PoolStringArray ks; + + for (const HashMap::Element *E = _header_parameters.front(); E; E = E->next) { + ks.push_back(E->key()); + } + + return ks; +} +PoolStringArray SimpleWebServerRequest::get_parameter_keys() const { + PoolStringArray g = get_get_parameter_keys(); + + g.append_array(get_post_parameter_keys()); + + return g; +} +PoolStringArray SimpleWebServerRequest::get_post_parameter_keys() const { + PoolStringArray ks; + + for (const HashMap::Element *E = _post_parameters.front(); E; E = E->next) { + ks.push_back(E->key()); + } + + return ks; +} +PoolStringArray SimpleWebServerRequest::get_get_parameter_keys() const { + PoolStringArray ks; + + for (const HashMap::Element *E = _get_parameters.front(); E; E = E->next) { + ks.push_back(E->key()); + } + + return ks; +} + void SimpleWebServerRequest::send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code) { ERR_FAIL_COND(!_server); diff --git a/modules/http_server_simple/simple_web_server_request.h b/modules/http_server_simple/simple_web_server_request.h index 2bed875ec..519354908 100644 --- a/modules/http_server_simple/simple_web_server_request.h +++ b/modules/http_server_simple/simple_web_server_request.h @@ -76,6 +76,11 @@ public: virtual String get_header_parameter(const String &key) const; virtual void set_header_parameter(const String &key, const String &value); + virtual PoolStringArray get_header_parameter_keys() const; + virtual PoolStringArray get_parameter_keys() const; + virtual PoolStringArray get_post_parameter_keys() const; + virtual PoolStringArray get_get_parameter_keys() const; + virtual void send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code = HTTPServerEnums::HTTP_STATUS_CODE_302_FOUND); virtual void send(); virtual void send_file(const String &p_file_path); diff --git a/modules/web/http/web_server_request.cpp b/modules/web/http/web_server_request.cpp index aaeb84863..4fe0de2ed 100644 --- a/modules/web/http/web_server_request.cpp +++ b/modules/web/http/web_server_request.cpp @@ -290,6 +290,19 @@ String WebServerRequest::get_header_parameter(const String &key) const { void WebServerRequest::set_header_parameter(const String &key, const String &value) { } +PoolStringArray WebServerRequest::get_header_parameter_keys() const { + return PoolStringArray(); +} +PoolStringArray WebServerRequest::get_parameter_keys() const { + return PoolStringArray(); +} +PoolStringArray WebServerRequest::get_post_parameter_keys() const { + return PoolStringArray(); +} +PoolStringArray WebServerRequest::get_get_parameter_keys() const { + return PoolStringArray(); +} + HTTPServerEnums::HTTPStatusCode WebServerRequest::get_status_code() const { return _status_code; } @@ -651,6 +664,11 @@ void WebServerRequest::_bind_methods() { ClassDB::bind_method(D_METHOD("get_header_parameter", "key"), &WebServerRequest::get_header_parameter); ClassDB::bind_method(D_METHOD("set_header_parameter", "key", "value"), &WebServerRequest::set_header_parameter); + ClassDB::bind_method(D_METHOD("get_header_parameter_keys"), &WebServerRequest::get_header_parameter_keys); + ClassDB::bind_method(D_METHOD("get_parameter_keys"), &WebServerRequest::get_parameter_keys); + ClassDB::bind_method(D_METHOD("get_post_parameter_keys"), &WebServerRequest::get_post_parameter_keys); + ClassDB::bind_method(D_METHOD("get_get_parameter_keys"), &WebServerRequest::get_get_parameter_keys); + 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"); diff --git a/modules/web/http/web_server_request.h b/modules/web/http/web_server_request.h index 28a0c456a..f30758b8a 100644 --- a/modules/web/http/web_server_request.h +++ b/modules/web/http/web_server_request.h @@ -128,6 +128,11 @@ public: virtual String get_header_parameter(const String &key) const; virtual void set_header_parameter(const String &key, const String &value); + virtual PoolStringArray get_header_parameter_keys() const; + virtual PoolStringArray get_parameter_keys() const; + virtual PoolStringArray get_post_parameter_keys() const; + virtual PoolStringArray get_get_parameter_keys() const; + HTTPServerEnums::HTTPStatusCode get_status_code() const; void set_status_code(const HTTPServerEnums::HTTPStatusCode status_code); diff --git a/modules/web/http/web_server_request_scriptable.cpp b/modules/web/http/web_server_request_scriptable.cpp index 51d348d49..3d73cf3a1 100644 --- a/modules/web/http/web_server_request_scriptable.cpp +++ b/modules/web/http/web_server_request_scriptable.cpp @@ -92,6 +92,19 @@ void WebServerRequestScriptable::set_header_parameter(const String &key, const S call("_set_header_parameter", key, value); } +PoolStringArray WebServerRequestScriptable::get_header_parameter_keys() const { + return const_cast(this)->call("_get_header_parameter_keys"); +} +PoolStringArray WebServerRequestScriptable::get_parameter_keys() const { + return const_cast(this)->call("_get_parameter_keys"); +} +PoolStringArray WebServerRequestScriptable::get_post_parameter_keys() const { + return const_cast(this)->call("_get_post_parameter_keys"); +} +PoolStringArray WebServerRequestScriptable::get_get_parameter_keys() const { + return const_cast(this)->call("_get_get_parameter_keys"); +} + void WebServerRequestScriptable::send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code) { call("_send_redirect", location, status_code); } @@ -186,6 +199,19 @@ String WebServerRequestScriptable::_get_header_parameter(const String &key) cons void WebServerRequestScriptable::_set_header_parameter(const String &key, const String &value) { } +PoolStringArray WebServerRequestScriptable::_get_header_parameter_keys() const { + return PoolStringArray(); +} +PoolStringArray WebServerRequestScriptable::_get_parameter_keys() const { + return PoolStringArray(); +} +PoolStringArray WebServerRequestScriptable::_get_post_parameter_keys() const { + return PoolStringArray(); +} +PoolStringArray WebServerRequestScriptable::_get_get_parameter_keys() const { + return PoolStringArray(); +} + void WebServerRequestScriptable::_send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code) { } @@ -287,6 +313,11 @@ void WebServerRequestScriptable::_bind_methods() { BIND_VMETHOD(MethodInfo("_get_header_parameter", PropertyInfo(Variant::STRING, "key"))); BIND_VMETHOD(MethodInfo("_set_header_parameter", PropertyInfo(Variant::STRING, "key"), PropertyInfo(Variant::STRING, "value"))); + BIND_VMETHOD(MethodInfo(Variant::POOL_STRING_ARRAY, "_get_header_parameter_keys")); + BIND_VMETHOD(MethodInfo(Variant::POOL_STRING_ARRAY, "_get_parameter_keys")); + BIND_VMETHOD(MethodInfo(Variant::POOL_STRING_ARRAY, "_get_post_parameter_keys")); + BIND_VMETHOD(MethodInfo(Variant::POOL_STRING_ARRAY, "_get_get_parameter_keys")); + BIND_VMETHOD(MethodInfo("_send_redirect", PropertyInfo(Variant::STRING, "location"), PropertyInfo(Variant::INT, "status_code"))); BIND_VMETHOD(MethodInfo("_compile_body")); @@ -323,6 +354,11 @@ void WebServerRequestScriptable::_bind_methods() { ClassDB::bind_method(D_METHOD("_get_header_parameter", "key"), &WebServerRequestScriptable::_get_header_parameter); ClassDB::bind_method(D_METHOD("_set_header_parameter", "key", "value"), &WebServerRequestScriptable::_set_header_parameter); + ClassDB::bind_method(D_METHOD("_get_header_parameter_keys"), &WebServerRequestScriptable::_get_header_parameter_keys); + ClassDB::bind_method(D_METHOD("_get_parameter_keys"), &WebServerRequestScriptable::_get_parameter_keys); + ClassDB::bind_method(D_METHOD("_get_post_parameter_keys"), &WebServerRequestScriptable::_get_post_parameter_keys); + ClassDB::bind_method(D_METHOD("_get_get_parameter_keys"), &WebServerRequestScriptable::_get_get_parameter_keys); + ClassDB::bind_method(D_METHOD("_send_redirect", "location", "status_code"), &WebServerRequestScriptable::_send_redirect); ClassDB::bind_method(D_METHOD("_compile_body"), &WebServerRequestScriptable::_compile_body); diff --git a/modules/web/http/web_server_request_scriptable.h b/modules/web/http/web_server_request_scriptable.h index 891d2c5b6..d9f34d539 100644 --- a/modules/web/http/web_server_request_scriptable.h +++ b/modules/web/http/web_server_request_scriptable.h @@ -75,6 +75,11 @@ public: virtual String get_header_parameter(const String &key) const; virtual void set_header_parameter(const String &key, const String &value); + virtual PoolStringArray get_header_parameter_keys() const; + virtual PoolStringArray get_parameter_keys() const; + virtual PoolStringArray get_post_parameter_keys() const; + virtual PoolStringArray get_get_parameter_keys() 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(); @@ -111,6 +116,11 @@ public: virtual String _get_header_parameter(const String &key) const; virtual void _set_header_parameter(const String &key, const String &value); + virtual PoolStringArray _get_header_parameter_keys() const; + virtual PoolStringArray _get_parameter_keys() const; + virtual PoolStringArray _get_post_parameter_keys() const; + virtual PoolStringArray _get_get_parameter_keys() const; + void _send_redirect(const String &location, const HTTPServerEnums::HTTPStatusCode status_code); void _compile_body(); void _compile_and_send_body();