add network.request

This commit is contained in:
MihailRis 2026-09-09 23:53:03 +03:00
parent 018e4c9f2c
commit 1c7bf2de01
2 changed files with 54 additions and 3 deletions

View file

@ -100,8 +100,29 @@ local _udp_client_open_callbacks = {}
local _http_response_callbacks = {} local _http_response_callbacks = {}
local _http_error_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) network.get = function(url, callback, errorCallback, headers)
local id = network.__get(url, headers) local id = http_get(url, headers)
if callback then if callback then
_http_response_callbacks[id] = callback _http_response_callbacks[id] = callback
end end
@ -111,7 +132,7 @@ network.get = function(url, callback, errorCallback, headers)
end end
network.get_binary = function(url, callback, errorCallback, headers) 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 if callback then
_http_response_callbacks[id] = callback _http_response_callbacks[id] = callback
end end
@ -121,7 +142,7 @@ network.get_binary = function(url, callback, errorCallback, headers)
end end
network.post = function(url, data, callback, errorCallback, headers) 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 if callback then
_http_response_callbacks[id] = callback _http_response_callbacks[id] = callback
end end

View file

@ -86,6 +86,35 @@ static std::vector<std::string> read_headers(lua::State* L, int index) {
static int request_id = 1; 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) { static int perform_get(lua::State* L, network::Network& network, bool binary) {
std::string url(lua::require_lstring(L, 1)); std::string url(lua::require_lstring(L, 1));
auto headers = read_headers(L, 2); auto headers = read_headers(L, 2);
@ -568,6 +597,7 @@ int wrap(lua_State* L) {
} }
const luaL_Reg networklib[] = { const luaL_Reg networklib[] = {
{"__request", wrap<l_request>},
{"__get", wrap<l_get>}, {"__get", wrap<l_get>},
{"__get_binary", wrap<l_get_binary>}, {"__get_binary", wrap<l_get_binary>},
{"__post", wrap<l_post>}, {"__post", wrap<l_post>},