This commit is contained in:
MihailRis 2026-09-10 20:38:50 +03:00
parent 9226f362a7
commit 946728e54e
3 changed files with 28 additions and 2 deletions

View file

@ -125,17 +125,21 @@ network.request = request
network.get = function(url, callback, errorCallback, headers)
return request(url, {
method = "GET",
headers = headers,
on_response = callback,
on_error = errorCallback,
follow_location = true,
})
end
network.get_binary = function(url, callback, errorCallback, headers)
return request(url, {
method = "GET",
headers = headers,
on_response = callback and (function (response) return callback(Bytearray(response)) end),
on_error = errorCallback
on_error = errorCallback,
follow_location = true,
})
end

View file

@ -111,6 +111,19 @@ static int l_request(lua::State* L, network::Network& network) {
}
int currentRequestId = request_id++;
request.onResponse = [currentRequestId](std::vector<char> bytes) {
push_event(NetworkEvent(
RESPONSE,
ResponseEventDto {200, false, currentRequestId, std::move(bytes)}
));
};
request.onReject = [currentRequestId](int code, std::vector<char> bytes) {
push_event(NetworkEvent(
RESPONSE,
ResponseEventDto {code, false, currentRequestId, std::move(bytes)}
));
};
network.request(std::move(request));
return lua::pushinteger(L, currentRequestId);
}

View file

@ -80,6 +80,9 @@ public:
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.81.0");
#ifndef NDEBUG
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
#endif
if (request.maxSize == 0) {
curl_easy_setopt(
curl, CURLOPT_MAXFILESIZE, std::numeric_limits<long>::max()
@ -119,7 +122,8 @@ public:
if(msg->msg == CURLMSG_DONE) {
curl_multi_remove_handle(multiHandle, curl);
}
int response;
int response = -1;
CURLcode result = msg->data.result;
curl_easy_getinfo(msg->easy_handle, CURLINFO_RESPONSE_CODE, &response);
if (response == HTTP_OK) {
long size;
@ -133,6 +137,11 @@ public:
if (onResponse) {
onResponse(std::move(buffer));
}
} else if (response == 0) {
logger.error() << curl_easy_strerror(result) << " (" << url << ")";
if (onReject) {
onReject(response, {});
}
} else {
logger.error()
<< "response code " << response << " (" << url << ")"