aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2019-04-02 17:25:53 -0300
committerHisham Muhammad <hisham@gobolinux.org>2019-04-03 10:44:51 -0300
commitf5eef8f276b86654a21cb80f5b8e28028abcd5fa (patch)
tree271eaa134929f2d05cbccb7003d54724be9106a0 /src
parentb3d36513baa15b5c7b4c1ad24f655e3469b83dc5 (diff)
downloadluarocks-f5eef8f276b86654a21cb80f5b8e28028abcd5fa.tar.gz
luarocks-f5eef8f276b86654a21cb80f5b8e28028abcd5fa.tar.bz2
luarocks-f5eef8f276b86654a21cb80f5b8e28028abcd5fa.zip
fs.lua: cache failures as well
Diffstat (limited to 'src')
-rw-r--r--src/luarocks/fs/lua.lua50
1 files changed, 34 insertions, 16 deletions
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua
index 313204b9..c3bf87e3 100644
--- a/src/luarocks/fs/lua.lua
+++ b/src/luarocks/fs/lua.lua
@@ -670,6 +670,10 @@ local redirect_protocols = {
670 670
671local function request(url, method, http, loop_control) 671local function request(url, method, http, loop_control)
672 local result = {} 672 local result = {}
673
674 if cfg.verbose then
675 print(method, url)
676 end
673 677
674 local proxy = os.getenv("http_proxy") 678 local proxy = os.getenv("http_proxy")
675 if type(proxy) ~= "string" then proxy = nil end 679 if type(proxy) ~= "string" then proxy = nil end
@@ -735,13 +739,28 @@ local function request(url, method, http, loop_control)
735end 739end
736 740
737local function write_timestamp(filename, data) 741local function write_timestamp(filename, data)
738 local utfd = io.open(filename, "w") 742 local fd = io.open(filename, "w")
739 if utfd then 743 if fd then
740 utfd:write(data) 744 fd:write(data)
741 utfd:close() 745 fd:close()
746 end
747end
748
749local function read_timestamp(filename)
750 local fd = io.open(filename, "r")
751 if fd then
752 local data = fd:read("*a")
753 fd:close()
754 return data
742 end 755 end
743end 756end
744 757
758local function fail_with_status(filename, status, headers)
759 write_timestamp(filename .. ".unixtime", os.time())
760 write_timestamp(filename .. ".status", status)
761 return nil, status, headers
762end
763
745-- @param url string: URL to fetch. 764-- @param url string: URL to fetch.
746-- @param filename string: local filename of the file to fetch. 765-- @param filename string: local filename of the file to fetch.
747-- @param http table: The library to use (http from LuaSocket or LuaSec) 766-- @param http table: The library to use (http from LuaSocket or LuaSec)
@@ -751,25 +770,22 @@ end
751-- nil, error message and optionally HTTPS error in case of errors. 770-- nil, error message and optionally HTTPS error in case of errors.
752local function http_request(url, filename, http, cache) 771local function http_request(url, filename, http, cache)
753 if cache then 772 if cache then
754 local tsfd = io.open(filename..".timestamp", "r") 773 local status = read_timestamp(filename..".status")
755 if tsfd then 774 local timestamp = read_timestamp(filename..".timestamp")
756 local timestamp = tsfd:read("*a") 775 if status or timestamp then
757 tsfd:close() 776 local unixtime = read_timestamp(filename..".unixtime")
758 local unixtime
759 local utfd = io.open(filename..".unixtime", "r")
760 if utfd then
761 unixtime = tonumber(utfd:read("*a"))
762 utfd:close()
763 end
764 if unixtime then 777 if unixtime then
765 if os.time() - unixtime < cfg.cache_timeout then 778 if os.time() - unixtime < cfg.cache_timeout then
779 if status then
780 return nil, status, {}
781 end
766 return true, nil, nil, true 782 return true, nil, nil, true
767 end 783 end
768 end 784 end
769 785
770 local result, status, headers, err = request(url, "HEAD", http) 786 local result, status, headers, err = request(url, "HEAD", http)
771 if not result then 787 if not result then
772 return nil, status, headers 788 return fail_with_status(filename, status, headers)
773 end 789 end
774 if status == 200 and headers["last-modified"] == timestamp then 790 if status == 200 and headers["last-modified"] == timestamp then
775 write_timestamp(filename .. ".unixtime", os.time()) 791 write_timestamp(filename .. ".unixtime", os.time())
@@ -779,7 +795,9 @@ local function http_request(url, filename, http, cache)
779 end 795 end
780 local result, status, headers, err = request(url, "GET", http) 796 local result, status, headers, err = request(url, "GET", http)
781 if not result then 797 if not result then
782 return nil, status, headers 798 if status then
799 return fail_with_status(filename, status, headers)
800 end
783 end 801 end
784 if cache and headers["last-modified"] then 802 if cache and headers["last-modified"] then
785 write_timestamp(filename .. ".timestamp", headers["last-modified"]) 803 write_timestamp(filename .. ".timestamp", headers["last-modified"])