add 'headers' argument to network.get, network.get_binary and network.post

This commit is contained in:
MihailRis
2025-09-08 23:46:37 +03:00
parent cda5bfaf40
commit 9871bf1292
3 changed files with 72 additions and 10 deletions
+39 -6
View File
@@ -12,7 +12,7 @@ static int l_get(lua::State* L, network::Network& network) {
auto onResponse = lua::create_lambda_nothrow(L);
network::OnReject onReject = nullptr;
if (lua::gettop(L) >= 3) {
if (!lua::isnoneornil(L, 3)) {
lua::pushvalue(L, 3);
auto callback = lua::create_lambda_nothrow(L);
onReject = [callback](int code) {
@@ -20,11 +20,22 @@ static int l_get(lua::State* L, network::Network& network) {
};
}
std::vector<std::string> 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);
}
}
network.get(url, [onResponse](std::vector<char> bytes) {
engine->postRunnable([=]() {
onResponse({std::string(bytes.data(), bytes.size())});
});
}, std::move(onReject));
}, std::move(onReject), std::move(headers));
return 0;
}
@@ -35,7 +46,7 @@ static int l_get_binary(lua::State* L, network::Network& network) {
auto onResponse = lua::create_lambda_nothrow(L);
network::OnReject onReject = nullptr;
if (lua::gettop(L) >= 3) {
if (!lua::isnoneornil(L, 3)) {
lua::pushvalue(L, 3);
auto callback = lua::create_lambda_nothrow(L);
onReject = [callback](int code) {
@@ -43,6 +54,17 @@ static int l_get_binary(lua::State* L, network::Network& network) {
};
}
std::vector<std::string> 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);
}
}
network.get(url, [onResponse](std::vector<char> bytes) {
auto buffer = std::make_shared<util::Buffer<ubyte>>(
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
@@ -50,7 +72,8 @@ static int l_get_binary(lua::State* L, network::Network& network) {
engine->postRunnable([=]() {
onResponse({buffer});
});
}, std::move(onReject));
}, std::move(onReject), std::move(headers));
return 0;
}
@@ -62,7 +85,7 @@ static int l_post(lua::State* L, network::Network& network) {
auto onResponse = lua::create_lambda_nothrow(L);
network::OnReject onReject = nullptr;
if (lua::gettop(L) >= 4) {
if (!lua::isnoneornil(L, 4)) {
lua::pushvalue(L, 4);
auto callback = lua::create_lambda_nothrow(L);
onReject = [callback](int code) {
@@ -77,6 +100,16 @@ static int l_post(lua::State* L, network::Network& network) {
string = json::stringify(data, false);
}
std::vector<std::string> 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);
}
}
engine->getNetwork().post(url, string, [onResponse](std::vector<char> bytes) {
auto buffer = std::make_shared<util::Buffer<ubyte>>(
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
@@ -84,7 +117,7 @@ static int l_post(lua::State* L, network::Network& network) {
engine->postRunnable([=]() {
onResponse({std::string(bytes.data(), bytes.size())});
});
}, std::move(onReject));
}, std::move(onReject), std::move(headers));
return 0;
}