diff --git a/src/logic/scripting/lua/libs/libnetwork.cpp b/src/logic/scripting/lua/libs/libnetwork.cpp index 96517c6d..18d26644 100644 --- a/src/logic/scripting/lua/libs/libnetwork.cpp +++ b/src/logic/scripting/lua/libs/libnetwork.cpp @@ -5,6 +5,19 @@ using namespace scripting; +static std::vector read_headers(lua::State* L, int index) { + std::vector headers; + if (lua::istable(L, index)) { + int len = lua::objlen(L, index); + for (int i = 1; i <= len; i++) { + lua::rawgeti(L, i, index); + headers.push_back(lua::tostring(L, -1)); + lua::pop(L); + } + } + return headers; +} + static int l_get(lua::State* L, network::Network& network) { std::string url(lua::require_lstring(L, 1)); @@ -20,16 +33,7 @@ static int l_get(lua::State* L, network::Network& network) { }; } - std::vector headers; - - if (lua::istable(L, 4)) { - int len = lua::objlen(L, 4); - for (int i = 1; i <= len; i++) { - lua::rawgeti(L, i, 4); - headers.push_back(lua::tostring(L, -1)); - lua::pop(L); - } - } + auto headers = read_headers(L, 4); network.get(url, [onResponse](std::vector bytes) { engine->postRunnable([=]() { @@ -54,16 +58,7 @@ static int l_get_binary(lua::State* L, network::Network& network) { }; } - std::vector headers; - - if (lua::istable(L, 4)) { - int len = lua::objlen(L, 4); - for (int i = 1; i <= len; i++) { - lua::rawgeti(L, i, 4); - headers.push_back(lua::tostring(L, -1)); - lua::pop(L); - } - } + auto headers = read_headers(L, 4); network.get(url, [onResponse](std::vector bytes) { auto buffer = std::make_shared>( @@ -100,15 +95,7 @@ static int l_post(lua::State* L, network::Network& network) { string = json::stringify(data, false); } - std::vector headers; - if (lua::istable(L, 5)) { - int len = lua::objlen(L, 5); - for (int i = 1; i <= len; i++) { - lua::rawgeti(L, i, 5); - headers.push_back(lua::tostring(L, -1)); - lua::pop(L); - } - } + auto headers = read_headers(L, 5); engine->getNetwork().post(url, string, [onResponse](std::vector bytes) { auto buffer = std::make_shared>(