From bc79c2344e226bdf833382b5ce51c47ddd536bf2 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Wed, 10 Mar 2021 09:38:27 +0100 Subject: [PATCH] CSM: Use server-like (and safe) HTTP API instead of Mainmenu-like --- builtin/client/util.lua | 20 ++++++++++++++++++++ src/script/lua_api/l_http.cpp | 14 ++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/builtin/client/util.lua b/builtin/client/util.lua index aea15e00f..440f99ebc 100644 --- a/builtin/client/util.lua +++ b/builtin/client/util.lua @@ -58,3 +58,23 @@ end function core.get_nearby_objects(radius) return core.get_objects_inside_radius(core.localplayer:get_pos(), radius) end + +-- HTTP callback interface + +function core.http_add_fetch(httpenv) + httpenv.fetch = function(req, callback) + local handle = httpenv.fetch_async(req) + + local function update_http_status() + local res = httpenv.fetch_async_get(handle) + if res.completed then + callback(res) + else + core.after(0, update_http_status) + end + end + core.after(0, update_http_status) + end + + return httpenv +end diff --git a/src/script/lua_api/l_http.cpp b/src/script/lua_api/l_http.cpp index 0bf9cfbad..5ea3b3f99 100644 --- a/src/script/lua_api/l_http.cpp +++ b/src/script/lua_api/l_http.cpp @@ -239,8 +239,18 @@ int ModApiHttp::l_get_http_api(lua_State *L) void ModApiHttp::Initialize(lua_State *L, int top) { #if USE_CURL - API_FCT(get_http_api); - API_FCT(request_http_api); + + bool isMainmenu = false; +#ifndef SERVER + isMainmenu = ModApiBase::getGuiEngine(L) != nullptr; +#endif + + if (isMainmenu) { + API_FCT(get_http_api); + } else { + API_FCT(request_http_api); + } + #endif }