diff --git a/res/scripts/classes.lua b/res/scripts/classes.lua index c6e25b0b9..0f9f1ce0a 100644 --- a/res/scripts/classes.lua +++ b/res/scripts/classes.lua @@ -101,17 +101,11 @@ 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 local open_tcp = network.__open_tcp local open_udp = network.__open_udp local connect_tcp = network.__connect_tcp local connect_udp = network.__connect_udp network.__request = nil -network.__get = nil -network.__get_binary = nil -network.__post = nil network.__open_tcp = nil network.__open_udp = nil network.__connect_tcp = nil @@ -130,33 +124,31 @@ end network.request = request network.get = function(url, callback, errorCallback, headers) - local id = http_get(url, headers) - if callback then - _http_response_callbacks[id] = callback - end - if errorCallback then - _http_error_callbacks[id] = errorCallback - end + return request(url, { + headers = headers, + on_response = callback, + on_error = errorCallback, + }) end network.get_binary = function(url, callback, errorCallback, headers) - local id = http_get_binary(url, headers) - if callback then - _http_response_callbacks[id] = callback - end - if errorCallback then - _http_error_callbacks[id] = errorCallback - end + return request(url, { + headers = headers, + on_response = callback and (function (response) return callback(Bytearray(response)) end), + on_error = errorCallback + }) end -network.post = function(url, data, callback, errorCallback, headers) - local id = http_post(url, data, headers) - if callback then - _http_response_callbacks[id] = callback - end - if errorCallback then - _http_error_callbacks[id] = errorCallback - end +network.post = function(url, body, callback, errorCallback, headers) + return request(url, { + method = "POST", + headers = table.extend({ + "Content-Type: application/json" + }, headers), + body = body, + on_response = callback, + on_error = errorCallback, + }) end network.tcp_open = function (port, handler) diff --git a/src/logic/scripting/lua/libs/libnetwork.cpp b/src/logic/scripting/lua/libs/libnetwork.cpp index 318dfcc7c..b10d12b4b 100644 --- a/src/logic/scripting/lua/libs/libnetwork.cpp +++ b/src/logic/scripting/lua/libs/libnetwork.cpp @@ -94,7 +94,7 @@ static int l_request(lua::State* L, network::Network& network) { 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)); + request.method = lua::require_string(L, -1); lua::pop(L); } if (lua::getfield(L, "headers", 2)) { @@ -115,82 +115,6 @@ static int l_request(lua::State* L, network::Network& network) { 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); - - int currentRequestId = request_id++; - - network::HttpRequest request {}; - request.url = std::move(url); - request.onResponse = - [currentRequestId, binary](std::vector bytes) { - push_event(NetworkEvent( - RESPONSE, - ResponseEventDto { - 200, binary, currentRequestId, std::move(bytes)} - )); - }; - request.onReject = - [currentRequestId, binary](int code, std::vector bytes) { - push_event(NetworkEvent( - RESPONSE, - ResponseEventDto { - code, binary, currentRequestId, std::move(bytes)} - )); - }; - request.headers = std::move(headers); - network.request(std::move(request)); - return lua::pushinteger(L, currentRequestId); -} - -static int l_get(lua::State* L, network::Network& network) { - return perform_get(L, network, false); -} - -static int l_get_binary(lua::State* L, network::Network& network) { - return perform_get(L, network, true); -} - -static int l_post(lua::State* L, network::Network& network) { - std::string url(lua::require_lstring(L, 1)); - auto data = lua::tovalue(L, 2); - - std::string string; - if (data.isString()) { - string = data.asString(); - } else { - string = json::stringify(data, false); - } - - auto headers = read_headers(L, 3); - int currentRequestId = request_id++; - - network::HttpRequest request {}; - request.url = std::move(url); - request.body = string; - request.headers = std::move(headers); - request.headers.emplace_back("Content-Type: application/json"); - request.onResponse = - [currentRequestId](std::vector bytes) { - push_event(NetworkEvent( - RESPONSE, - ResponseEventDto { - 200, false, currentRequestId, std::move(bytes)} - )); - }; - request.onReject = - [currentRequestId](int code, std::vector bytes) { - push_event(NetworkEvent( - RESPONSE, - ResponseEventDto { - code, false, currentRequestId, std::move(bytes)} - )); - }; - network.request(std::move(request)); - return lua::pushinteger(L, currentRequestId); -} - static int l_close(lua::State* L, network::Network& network) { u64id_t id = lua::tointeger(L, 1); if (auto connection = network.getConnection(id, false)) { @@ -598,9 +522,6 @@ int wrap(lua_State* L) { const luaL_Reg networklib[] = { {"__request", wrap}, - {"__get", wrap}, - {"__get_binary", wrap}, - {"__post", wrap}, {"get_total_upload", wrap}, {"get_total_download", wrap}, {"find_free_port", wrap},