From 4396b504b7848081322cbaa9d5e26c9641a161b4 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sat, 3 May 2014 16:53:41 -0300 Subject: Set timeout in LuaSocket/LuaSec based implementation as well. Be more forgiving with thi cfg.connection_timeout setting. Add --timeout flag, as discussed in #249. --- src/luarocks/command_line.lua | 9 +++++++++ src/luarocks/fs/lua.lua | 3 +++ src/luarocks/fs/unix/tools.lua | 4 ++-- src/luarocks/fs/win32/tools.lua | 4 ++-- src/luarocks/help.lua | 5 ++++- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/luarocks/command_line.lua b/src/luarocks/command_line.lua index 3cde4c41..cc2e1683 100644 --- a/src/luarocks/command_line.lua +++ b/src/luarocks/command_line.lua @@ -83,6 +83,15 @@ function command_line.run_command(...) fs.verbose() end + if flags["timeout"] then -- setting it in the config file will kick-in earlier in the process + local timeout = tonumber(flags["timeout"]) + if timeout then + cfg.connection_timeout = timeout + else + die "Argument error: --timeout expects a numeric argument." + end + end + if flags["version"] then util.printout(program.." "..cfg.program_version) util.printout(program_description) diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index cd705eef..f18deac3 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -551,6 +551,9 @@ local function request(url, method, http, loop_control) io.write(method.." "..url.." ...\n") end local dots = 0 + if cfg.connection_timeout and cfg.connection_timeout > 0 then + http.TIMEOUT = cfg.connection_timeout + end local res, status, headers, err = http.request { url = url, proxy = proxy, diff --git a/src/luarocks/fs/unix/tools.lua b/src/luarocks/fs/unix/tools.lua index 88189e07..f8f39eca 100644 --- a/src/luarocks/fs/unix/tools.lua +++ b/src/luarocks/fs/unix/tools.lua @@ -247,7 +247,7 @@ function tools.download(url, filename, cache) local ok if cfg.downloader == "wget" then local wget_cmd = fs.Q(vars.WGET).." --no-check-certificate --no-cache --user-agent='"..cfg.user_agent.." via wget' --quiet " - if cfg.connection_timeout > 0 then + if cfg.connection_timeout and cfg.connection_timeout > 0 then wget_cmd = wget_cmd .. "--timeout="..tonumber(cfg.connection_timeout).." --tries=1 " end if cache then @@ -263,7 +263,7 @@ function tools.download(url, filename, cache) end elseif cfg.downloader == "curl" then local curl_cmd = fs.Q(vars.CURL).." -f -k -L --user-agent '"..cfg.user_agent.." via curl' " - if cfg.connection_timeout > 0 then + if cfg.connection_timeout and cfg.connection_timeout > 0 then curl_cmd = curl_cmd .. "--connect-timeout="..tonumber(cfg.connection_timeout).." " end ok = fs.execute_string(curl_cmd..fs.Q(url).." 2> /dev/null 1> "..fs.Q(filename)) diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index 9f32bf5b..21d846b9 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua @@ -257,7 +257,7 @@ function tools.download(url, filename, cache) local ok if cfg.downloader == "wget" then local wget_cmd = fs.Q(vars.WGET).." --no-check-certificate --no-cache --user-agent=\""..cfg.user_agent.." via wget\" --quiet " - if cfg.connection_timeout > 0 then + if cfg.connection_timeout and cfg.connection_timeout > 0 then wget_cmd = wget_cmd .. "--timeout="..tonumber(cfg.connection_timeout).." --tries=1 " end if cache then @@ -273,7 +273,7 @@ function tools.download(url, filename, cache) end elseif cfg.downloader == "curl" then local curl_cmd = vars.CURL.." -f -k -L --user-agent \""..cfg.user_agent.." via curl\" " - if cfg.connection_timeout > 0 then + if cfg.connection_timeout and cfg.connection_timeout > 0 then curl_cmd = curl_cmd .. "--connect-timeout="..tonumber(cfg.connection_timeout).." " end ok = fs.execute_string(curl_cmd..fs.Q(url).." 2> NUL 1> "..fs.Q(filename)) diff --git a/src/luarocks/help.lua b/src/luarocks/help.lua index 3e428a4a..0a155509 100644 --- a/src/luarocks/help.lua +++ b/src/luarocks/help.lua @@ -66,7 +66,10 @@ function help.run(...) --tree= Which tree to operate on. --local Use the tree in the user's home directory. To enable it, see ']]..program..[[ help path'. - --verbose Display verbose output of commands executed.]]) + --verbose Display verbose output of commands executed. + --timeout= Timeout on network operations, in seconds. + 0 means no timeout (wait forever). + Default is ]]..cfg.connection_timeout..[[.]]) print_section("VARIABLES") util.printout([[ Variables from the "variables" table of the configuration file -- cgit v1.2.3-55-g6feb