diff --git a/res/scripts/classes.lua b/res/scripts/classes.lua index 0f9f1ce0a..b044aa478 100644 --- a/res/scripts/classes.lua +++ b/res/scripts/classes.lua @@ -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 diff --git a/src/logic/scripting/lua/libs/libnetwork.cpp b/src/logic/scripting/lua/libs/libnetwork.cpp index b10d12b4b..d8c9e7c98 100644 --- a/src/logic/scripting/lua/libs/libnetwork.cpp +++ b/src/logic/scripting/lua/libs/libnetwork.cpp @@ -111,6 +111,19 @@ static int l_request(lua::State* L, network::Network& network) { } int currentRequestId = request_id++; + 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); } diff --git a/src/network/Curl.cpp b/src/network/Curl.cpp index 872e6d680..3bbeb3489 100644 --- a/src/network/Curl.cpp +++ b/src/network/Curl.cpp @@ -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::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 << ")"