commit
1a0373a3c5
@ -6,9 +6,15 @@ A library for working with the network.
|
|||||||
|
|
||||||
```lua
|
```lua
|
||||||
-- Performs a GET request to the specified URL.
|
-- Performs a GET request to the specified URL.
|
||||||
-- After receiving the response, passes the text to the callback function.
|
network.get(
|
||||||
-- In case of an error, the HTTP response code will be passed to onfailure.
|
url: str,
|
||||||
network.get(url: str, callback: function(str), [optional] onfailure: function(int))
|
-- Function to call when response is received
|
||||||
|
callback: function(str),
|
||||||
|
-- Error handler
|
||||||
|
[optional] onfailure: function(int),
|
||||||
|
-- List of additional request headers
|
||||||
|
[optional] headers: table<string>
|
||||||
|
)
|
||||||
|
|
||||||
-- Example:
|
-- Example:
|
||||||
network.get("https://api.github.com/repos/MihailRis/VoxelEngine-Cpp/releases/latest", function (s)
|
network.get("https://api.github.com/repos/MihailRis/VoxelEngine-Cpp/releases/latest", function (s)
|
||||||
@ -16,13 +22,28 @@ network.get("https://api.github.com/repos/MihailRis/VoxelEngine-Cpp/releases/lat
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
-- A variant for binary files, with a byte array instead of a string in the response.
|
-- A variant for binary files, with a byte array instead of a string in the response.
|
||||||
network.get_binary(url: str, callback: function(table|ByteArray), [optional] onfailure: function(int))
|
network.get_binary(
|
||||||
|
url: str,
|
||||||
|
callback: function(table|ByteArray),
|
||||||
|
[optional] onfailure: function(int),
|
||||||
|
[optional] headers: table<string>
|
||||||
|
)
|
||||||
|
|
||||||
-- Performs a POST request to the specified URL.
|
-- Performs a POST request to the specified URL.
|
||||||
-- Currently, only `Content-Type: application/json` is supported
|
-- Currently, only `Content-Type: application/json` is supported
|
||||||
-- After receiving the response, passes the text to the callback function.
|
-- After receiving the response, passes the text to the callback function.
|
||||||
-- In case of an error, the HTTP response code will be passed to onfailure.
|
-- In case of an error, the HTTP response code will be passed to onfailure.
|
||||||
network.post(url: str, data: table, callback: function(str), [optional] onfailure: function(int))
|
network.post(
|
||||||
|
url: str,
|
||||||
|
-- Request body as a table (will be converted to JSON) or string
|
||||||
|
body: table|string,
|
||||||
|
-- Function called when response is received
|
||||||
|
callback: function(str),
|
||||||
|
-- Error handler
|
||||||
|
[optional] onfailure: function(int),
|
||||||
|
-- List of additional request headers
|
||||||
|
[optional] headers: table<string>
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
## TCP Connections
|
## TCP Connections
|
||||||
|
|||||||
@ -6,9 +6,15 @@
|
|||||||
|
|
||||||
```lua
|
```lua
|
||||||
-- Выполняет GET запрос к указанному URL.
|
-- Выполняет GET запрос к указанному URL.
|
||||||
-- После получения ответа, передаёт текст в функцию callback.
|
network.get(
|
||||||
-- В случае ошибки в onfailure будет передан HTTP-код ответа.
|
url: str,
|
||||||
network.get(url: str, callback: function(str), [опционально] onfailure: function(int))
|
-- Функция, вызываемая при получении ответа
|
||||||
|
callback: function(str),
|
||||||
|
-- Обработчик ошибок
|
||||||
|
[опционально] onfailure: function(int),
|
||||||
|
-- Список дополнительных заголовков запроса
|
||||||
|
[опционально] headers: table<string>
|
||||||
|
)
|
||||||
|
|
||||||
-- Пример:
|
-- Пример:
|
||||||
network.get("https://api.github.com/repos/MihailRis/VoxelEngine-Cpp/releases/latest", function (s)
|
network.get("https://api.github.com/repos/MihailRis/VoxelEngine-Cpp/releases/latest", function (s)
|
||||||
@ -16,13 +22,28 @@ network.get("https://api.github.com/repos/MihailRis/VoxelEngine-Cpp/releases/lat
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
-- Вариант для двоичных файлов, с массивом байт вместо строки в ответе.
|
-- Вариант для двоичных файлов, с массивом байт вместо строки в ответе.
|
||||||
network.get_binary(url: str, callback: function(table|ByteArray), [опционально] onfailure: function(int))
|
network.get_binary(
|
||||||
|
url: str,
|
||||||
|
callback: function(table|ByteArray),
|
||||||
|
[опционально] onfailure: function(int),
|
||||||
|
[опционально] headers: table<string>
|
||||||
|
)
|
||||||
|
|
||||||
-- Выполняет POST запрос к указанному URL.
|
-- Выполняет POST запрос к указанному URL.
|
||||||
-- На данный момент реализована поддержка только `Content-Type: application/json`
|
-- На данный момент реализована поддержка только `Content-Type: application/json`
|
||||||
-- После получения ответа, передаёт текст в функцию callback.
|
-- После получения ответа, передаёт текст в функцию callback.
|
||||||
-- В случае ошибки в onfailure будет передан HTTP-код ответа.
|
-- В случае ошибки в onfailure будет передан HTTP-код ответа.
|
||||||
network.post(url: str, data: table, callback: function(str), [опционально] onfailure: function(int))
|
network.post(
|
||||||
|
url: str,
|
||||||
|
-- Тело запроса в виде таблицы, конвертируемой в JSON или строки
|
||||||
|
body: table|string,
|
||||||
|
-- Функция, вызываемая при получении ответа
|
||||||
|
callback: function(str),
|
||||||
|
-- Обработчик ошибок
|
||||||
|
[опционально] onfailure: function(int),
|
||||||
|
-- Список дополнительных заголовков запроса
|
||||||
|
[опционально] headers: table<string>
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
## TCP-Соединения
|
## TCP-Соединения
|
||||||
|
|||||||
@ -5,6 +5,19 @@
|
|||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|
||||||
|
static std::vector<std::string> read_headers(lua::State* L, int index) {
|
||||||
|
std::vector<std::string> 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) {
|
static int l_get(lua::State* L, network::Network& network) {
|
||||||
std::string url(lua::require_lstring(L, 1));
|
std::string url(lua::require_lstring(L, 1));
|
||||||
|
|
||||||
@ -12,7 +25,7 @@ static int l_get(lua::State* L, network::Network& network) {
|
|||||||
auto onResponse = lua::create_lambda_nothrow(L);
|
auto onResponse = lua::create_lambda_nothrow(L);
|
||||||
|
|
||||||
network::OnReject onReject = nullptr;
|
network::OnReject onReject = nullptr;
|
||||||
if (lua::gettop(L) >= 3) {
|
if (!lua::isnoneornil(L, 3)) {
|
||||||
lua::pushvalue(L, 3);
|
lua::pushvalue(L, 3);
|
||||||
auto callback = lua::create_lambda_nothrow(L);
|
auto callback = lua::create_lambda_nothrow(L);
|
||||||
onReject = [callback](int code) {
|
onReject = [callback](int code) {
|
||||||
@ -20,11 +33,13 @@ static int l_get(lua::State* L, network::Network& network) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto headers = read_headers(L, 4);
|
||||||
|
|
||||||
network.get(url, [onResponse](std::vector<char> bytes) {
|
network.get(url, [onResponse](std::vector<char> bytes) {
|
||||||
engine->postRunnable([=]() {
|
engine->postRunnable([=]() {
|
||||||
onResponse({std::string(bytes.data(), bytes.size())});
|
onResponse({std::string(bytes.data(), bytes.size())});
|
||||||
});
|
});
|
||||||
}, std::move(onReject));
|
}, std::move(onReject), std::move(headers));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +50,7 @@ static int l_get_binary(lua::State* L, network::Network& network) {
|
|||||||
auto onResponse = lua::create_lambda_nothrow(L);
|
auto onResponse = lua::create_lambda_nothrow(L);
|
||||||
|
|
||||||
network::OnReject onReject = nullptr;
|
network::OnReject onReject = nullptr;
|
||||||
if (lua::gettop(L) >= 3) {
|
if (!lua::isnoneornil(L, 3)) {
|
||||||
lua::pushvalue(L, 3);
|
lua::pushvalue(L, 3);
|
||||||
auto callback = lua::create_lambda_nothrow(L);
|
auto callback = lua::create_lambda_nothrow(L);
|
||||||
onReject = [callback](int code) {
|
onReject = [callback](int code) {
|
||||||
@ -43,6 +58,8 @@ static int l_get_binary(lua::State* L, network::Network& network) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto headers = read_headers(L, 4);
|
||||||
|
|
||||||
network.get(url, [onResponse](std::vector<char> bytes) {
|
network.get(url, [onResponse](std::vector<char> bytes) {
|
||||||
auto buffer = std::make_shared<util::Buffer<ubyte>>(
|
auto buffer = std::make_shared<util::Buffer<ubyte>>(
|
||||||
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
|
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
|
||||||
@ -50,7 +67,8 @@ static int l_get_binary(lua::State* L, network::Network& network) {
|
|||||||
engine->postRunnable([=]() {
|
engine->postRunnable([=]() {
|
||||||
onResponse({buffer});
|
onResponse({buffer});
|
||||||
});
|
});
|
||||||
}, std::move(onReject));
|
}, std::move(onReject), std::move(headers));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +80,7 @@ static int l_post(lua::State* L, network::Network& network) {
|
|||||||
auto onResponse = lua::create_lambda_nothrow(L);
|
auto onResponse = lua::create_lambda_nothrow(L);
|
||||||
|
|
||||||
network::OnReject onReject = nullptr;
|
network::OnReject onReject = nullptr;
|
||||||
if (lua::gettop(L) >= 4) {
|
if (!lua::isnoneornil(L, 4)) {
|
||||||
lua::pushvalue(L, 4);
|
lua::pushvalue(L, 4);
|
||||||
auto callback = lua::create_lambda_nothrow(L);
|
auto callback = lua::create_lambda_nothrow(L);
|
||||||
onReject = [callback](int code) {
|
onReject = [callback](int code) {
|
||||||
@ -77,6 +95,8 @@ static int l_post(lua::State* L, network::Network& network) {
|
|||||||
string = json::stringify(data, false);
|
string = json::stringify(data, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto headers = read_headers(L, 5);
|
||||||
|
|
||||||
engine->getNetwork().post(url, string, [onResponse](std::vector<char> bytes) {
|
engine->getNetwork().post(url, string, [onResponse](std::vector<char> bytes) {
|
||||||
auto buffer = std::make_shared<util::Buffer<ubyte>>(
|
auto buffer = std::make_shared<util::Buffer<ubyte>>(
|
||||||
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
|
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
|
||||||
@ -84,7 +104,7 @@ static int l_post(lua::State* L, network::Network& network) {
|
|||||||
engine->postRunnable([=]() {
|
engine->postRunnable([=]() {
|
||||||
onResponse({std::string(bytes.data(), bytes.size())});
|
onResponse({std::string(bytes.data(), bytes.size())});
|
||||||
});
|
});
|
||||||
}, std::move(onReject));
|
}, std::move(onReject), std::move(headers));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -57,6 +57,7 @@ struct Request {
|
|||||||
long maxSize;
|
long maxSize;
|
||||||
bool followLocation = false;
|
bool followLocation = false;
|
||||||
std::string data;
|
std::string data;
|
||||||
|
std::vector<std::string> headers;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CurlRequests : public Requests {
|
class CurlRequests : public Requests {
|
||||||
@ -86,10 +87,18 @@ public:
|
|||||||
const std::string& url,
|
const std::string& url,
|
||||||
OnResponse onResponse,
|
OnResponse onResponse,
|
||||||
OnReject onReject,
|
OnReject onReject,
|
||||||
|
std::vector<std::string> headers,
|
||||||
long maxSize
|
long maxSize
|
||||||
) override {
|
) override {
|
||||||
Request request {
|
Request request {
|
||||||
RequestType::GET, url, onResponse, onReject, maxSize, false, ""};
|
RequestType::GET,
|
||||||
|
url,
|
||||||
|
onResponse,
|
||||||
|
onReject,
|
||||||
|
maxSize,
|
||||||
|
false,
|
||||||
|
"",
|
||||||
|
std::move(headers)};
|
||||||
processRequest(std::move(request));
|
processRequest(std::move(request));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,10 +107,18 @@ public:
|
|||||||
const std::string& data,
|
const std::string& data,
|
||||||
OnResponse onResponse,
|
OnResponse onResponse,
|
||||||
OnReject onReject=nullptr,
|
OnReject onReject=nullptr,
|
||||||
|
std::vector<std::string> headers = {},
|
||||||
long maxSize=0
|
long maxSize=0
|
||||||
) override {
|
) override {
|
||||||
Request request {
|
Request request {
|
||||||
RequestType::POST, url, onResponse, onReject, maxSize, false, ""};
|
RequestType::POST,
|
||||||
|
url,
|
||||||
|
onResponse,
|
||||||
|
onReject,
|
||||||
|
maxSize,
|
||||||
|
false,
|
||||||
|
"",
|
||||||
|
std::move(headers)};
|
||||||
request.data = data;
|
request.data = data;
|
||||||
processRequest(std::move(request));
|
processRequest(std::move(request));
|
||||||
}
|
}
|
||||||
@ -121,6 +138,10 @@ public:
|
|||||||
curl_easy_setopt(curl, CURLOPT_POST, request.type == RequestType::POST);
|
curl_easy_setopt(curl, CURLOPT_POST, request.type == RequestType::POST);
|
||||||
|
|
||||||
curl_slist* hs = NULL;
|
curl_slist* hs = NULL;
|
||||||
|
|
||||||
|
for (const auto& header : request.headers) {
|
||||||
|
hs = curl_slist_append(hs, header.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
switch (request.type) {
|
switch (request.type) {
|
||||||
case RequestType::GET:
|
case RequestType::GET:
|
||||||
@ -817,9 +838,10 @@ void Network::get(
|
|||||||
const std::string& url,
|
const std::string& url,
|
||||||
OnResponse onResponse,
|
OnResponse onResponse,
|
||||||
OnReject onReject,
|
OnReject onReject,
|
||||||
|
std::vector<std::string> headers,
|
||||||
long maxSize
|
long maxSize
|
||||||
) {
|
) {
|
||||||
requests->get(url, onResponse, onReject, maxSize);
|
requests->get(url, onResponse, onReject, std::move(headers), maxSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::post(
|
void Network::post(
|
||||||
@ -827,9 +849,12 @@ void Network::post(
|
|||||||
const std::string& fieldsData,
|
const std::string& fieldsData,
|
||||||
OnResponse onResponse,
|
OnResponse onResponse,
|
||||||
OnReject onReject,
|
OnReject onReject,
|
||||||
|
std::vector<std::string> headers,
|
||||||
long maxSize
|
long maxSize
|
||||||
) {
|
) {
|
||||||
requests->post(url, fieldsData, onResponse, onReject, maxSize);
|
requests->post(
|
||||||
|
url, fieldsData, onResponse, onReject, std::move(headers), maxSize
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection* Network::getConnection(u64id_t id) {
|
Connection* Network::getConnection(u64id_t id) {
|
||||||
|
|||||||
@ -25,6 +25,7 @@ namespace network {
|
|||||||
const std::string& url,
|
const std::string& url,
|
||||||
OnResponse onResponse,
|
OnResponse onResponse,
|
||||||
OnReject onReject=nullptr,
|
OnReject onReject=nullptr,
|
||||||
|
std::vector<std::string> headers = {},
|
||||||
long maxSize=0
|
long maxSize=0
|
||||||
) = 0;
|
) = 0;
|
||||||
|
|
||||||
@ -33,6 +34,7 @@ namespace network {
|
|||||||
const std::string& data,
|
const std::string& data,
|
||||||
OnResponse onResponse,
|
OnResponse onResponse,
|
||||||
OnReject onReject=nullptr,
|
OnReject onReject=nullptr,
|
||||||
|
std::vector<std::string> headers = {},
|
||||||
long maxSize=0
|
long maxSize=0
|
||||||
) = 0;
|
) = 0;
|
||||||
|
|
||||||
@ -144,6 +146,7 @@ namespace network {
|
|||||||
const std::string& url,
|
const std::string& url,
|
||||||
OnResponse onResponse,
|
OnResponse onResponse,
|
||||||
OnReject onReject = nullptr,
|
OnReject onReject = nullptr,
|
||||||
|
std::vector<std::string> headers = {},
|
||||||
long maxSize=0
|
long maxSize=0
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -152,6 +155,7 @@ namespace network {
|
|||||||
const std::string& fieldsData,
|
const std::string& fieldsData,
|
||||||
OnResponse onResponse,
|
OnResponse onResponse,
|
||||||
OnReject onReject = nullptr,
|
OnReject onReject = nullptr,
|
||||||
|
std::vector<std::string> headers = {},
|
||||||
long maxSize=0
|
long maxSize=0
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user