From 5d84016fed6c88bb19557abed838a85e2e10bfe7 Mon Sep 17 00:00:00 2001 From: cyberbit Date: Tue, 5 Nov 2024 01:37:46 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20installer=20not=20followin?= =?UTF-8?q?g=20redirects=20on=20MC=201.12=20(#76)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/telem/bin/install.lua | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/telem/bin/install.lua b/src/telem/bin/install.lua index e3d01c7..70701c9 100644 --- a/src/telem/bin/install.lua +++ b/src/telem/bin/install.lua @@ -36,6 +36,33 @@ local ui = (function () return b end)() +-- patch redirect support for CC:T on 1.12 (and earlier?) +local function httpGetRedirect(url, maxRedirects) + maxRedirects = maxRedirects or 5 + local redirects = 0 + + while redirects < maxRedirects do + local response = http.get(url) + if not response then + return nil, 'Failed to get response' + end + + local statusCode = response.getResponseCode() + if statusCode >= 300 and statusCode < 400 then + local location = response.getResponseHeaders()['Location'] + if not location then + return nil, 'Redirect location not provided' + end + url = location + redirects = redirects + 1 + else + return response + end + end + + return nil, 'Too many redirects' +end + local termW, termH = term.getSize() local boxSizing = { @@ -84,7 +111,7 @@ local youWouldntDownloadATree = function (tree, updateProgress, updateBlob) local bloburl = string.gsub(tree.url, '{sha}', tree.sha) bloburl = string.gsub(bloburl, '{path}', v.path) - local blobreq = http.get(bloburl) + local blobreq = httpGetRedirect(bloburl) local fout = fs.open(physicalPath, 'w') fout.write(blobreq.readAll()) @@ -127,7 +154,7 @@ local showReleaseSelector = function () local releaseUrl = 'https://get.telem.cc/blob/releases' - local req = http.get(releaseUrl) + local req = httpGetRedirect(releaseUrl) local jres = textutils.unserialiseJSON(req.readAll()) local nonPreReleases = {} @@ -283,7 +310,7 @@ local installActions = { local treeUrl = 'https://get.telem.cc/blob/lib/main' - local req = http.get(treeUrl) + local req = httpGetRedirect(treeUrl) local res = textutils.unserialiseJSON(req.readAll()) youWouldntDownloadATree(res, progress, currentBlob) @@ -292,7 +319,7 @@ local installActions = { treeUrl = 'https://get.telem.cc/blob/vendor/main' - req = http.get(treeUrl) + req = httpGetRedirect(treeUrl) res = textutils.unserialiseJSON(req.readAll()) youWouldntDownloadATree(res, progress, currentBlob)