diff --git a/src/lua/LuaScriptInterface.cpp b/src/lua/LuaScriptInterface.cpp index 9ccf46ebf..3869c005a 100644 --- a/src/lua/LuaScriptInterface.cpp +++ b/src/lua/LuaScriptInterface.cpp @@ -3819,25 +3819,29 @@ static int http_request_finish(lua_State *l) return 0; } -static int http_request(lua_State *l) +static int http_request(lua_State *l, bool isPost) { ByteString uri(luaL_checkstring(l, 1)); std::map post_data; - if (lua_istable(l, 2)) + if (isPost) { - lua_pushnil(l); - while (lua_next(l, 2)) + if (lua_istable(l, 2)) { - lua_pushvalue(l, -2); - post_data.emplace(lua_tostring(l, -1), lua_tostring(l, -2)); - lua_pop(l, 2); + lua_pushnil(l); + while (lua_next(l, 2)) + { + lua_pushvalue(l, -2); + post_data.emplace(lua_tostring(l, -1), lua_tostring(l, -2)); + lua_pop(l, 2); + } } } + std::map headers; - if (lua_istable(l, 3)) + if (lua_istable(l, isPost ? 3 : 2)) { lua_pushnil(l); - while (lua_next(l, 3)) + while (lua_next(l, isPost ? 3 : 2)) { lua_pushvalue(l, -2); headers.emplace(lua_tostring(l, -1), lua_tostring(l, -2)); @@ -3855,6 +3859,17 @@ static int http_request(lua_State *l) return 1; } + +int LuaScriptInterface::http_get(lua_State * l) +{ + return http_request(l, false); +} + +int LuaScriptInterface::http_post(lua_State * l) +{ + return http_request(l, true); +} + void LuaScriptInterface::initHttpAPI() { luaL_newmetatable(l, "HTTPRequest"); @@ -3871,7 +3886,8 @@ void LuaScriptInterface::initHttpAPI() lua_setfield(l, -2, "finish"); lua_setfield(l, -2, "__index"); struct luaL_Reg httpAPIMethods [] = { - {"request", http_request}, + {"get", http_get}, + {"post", http_post}, {NULL, NULL} }; luaL_register(l, "http", httpAPIMethods); diff --git a/src/lua/LuaScriptInterface.h b/src/lua/LuaScriptInterface.h index 767a64e1b..20097b9e6 100644 --- a/src/lua/LuaScriptInterface.h +++ b/src/lua/LuaScriptInterface.h @@ -181,6 +181,8 @@ class LuaScriptInterface: public CommandInterface static int event_getmodifiers(lua_State * l); void initHttpAPI(); + static int http_get(lua_State * l); + static int http_post(lua_State * l); std::vector lua_el_func_v, lua_gr_func_v, lua_cd_func_v; std::vector lua_el_mode_v;