From 1c7bf2de01f46a70dbaaa9d85d8e6eaa2e7b5a8d Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 9 Sep 2026 23:53:03 +0300 Subject: [PATCH] add network.request --- res/scripts/classes.lua | 27 ++++++++++++++++--- src/logic/scripting/lua/libs/libnetwork.cpp | 30 +++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/res/scripts/classes.lua b/res/scripts/classes.lua index 280c5ddeb..008d51d2f 100644 --- a/res/scripts/classes.lua +++ b/res/scripts/classes.lua @@ -100,8 +100,29 @@ local _udp_client_open_callbacks = {} local _http_response_callbacks = {} local _http_error_callbacks = {} +local http_request = network.__request +local http_get = network.__get +local http_get_binary = network.__get_binary +local http_post = network.__post +network.__request = nil +network.__get = nil +network.__get_binary = nil +network.__post = nil + +local function request(url, params) + local id = http_request(url, params) + if params.on_response then + _http_response_callbacks[id] = params.on_response + end + if params.on_error then + _http_error_callbacks[id] = params.on_error + end +end + +network.request = request + network.get = function(url, callback, errorCallback, headers) - local id = network.__get(url, headers) + local id = http_get(url, headers) if callback then _http_response_callbacks[id] = callback end @@ -111,7 +132,7 @@ network.get = function(url, callback, errorCallback, headers) end network.get_binary = function(url, callback, errorCallback, headers) - local id = network.__get_binary(url, headers) + local id = http_get_binary(url, headers) if callback then _http_response_callbacks[id] = callback end @@ -121,7 +142,7 @@ network.get_binary = function(url, callback, errorCallback, headers) end network.post = function(url, data, callback, errorCallback, headers) - local id = network.__post(url, data, headers) + local id = http_post(url, data, headers) if callback then _http_response_callbacks[id] = callback end diff --git a/src/logic/scripting/lua/libs/libnetwork.cpp b/src/logic/scripting/lua/libs/libnetwork.cpp index 1493eb0f0..318dfcc7c 100644 --- a/src/logic/scripting/lua/libs/libnetwork.cpp +++ b/src/logic/scripting/lua/libs/libnetwork.cpp @@ -86,6 +86,35 @@ static std::vector read_headers(lua::State* L, int index) { static int request_id = 1; +static int l_request(lua::State* L, network::Network& network) { + network::HttpRequest request {}; + request.url = lua::require_lstring(L, 1); + + if (!lua::istable(L, 2)) { + throw std::runtime_error("table expected as argument #2"); + } + if (lua::getfield(L, "method", 2)) { + request.method = util::upper_case(lua::require_string(L, -1)); + lua::pop(L); + } + if (lua::getfield(L, "headers", 2)) { + request.headers = read_headers(L, -1); + lua::pop(L); + } + if (lua::getfield(L, "body", 2)) { + request.body = lua::require_lstring(L, -1); + lua::pop(L); + } + if (lua::getfield(L, "follow_location", 2)) { + request.followLocation = lua::toboolean(L, -1); + lua::pop(L); + } + + int currentRequestId = request_id++; + network.request(std::move(request)); + return lua::pushinteger(L, currentRequestId); +} + static int perform_get(lua::State* L, network::Network& network, bool binary) { std::string url(lua::require_lstring(L, 1)); auto headers = read_headers(L, 2); @@ -568,6 +597,7 @@ int wrap(lua_State* L) { } const luaL_Reg networklib[] = { + {"__request", wrap}, {"__get", wrap}, {"__get_binary", wrap}, {"__post", wrap},