From 24af3a8dd304b0e56c1bfe78ba51651dba50b765 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Tue, 21 Dec 2010 11:34:37 -0200 Subject: Add HTTPS support to native fs.download function, using LuaSec. --- src/luarocks/fetch.lua | 4 +-- src/luarocks/fs/lua.lua | 84 +++++++++++++++++-------------------------------- 2 files changed, 31 insertions(+), 57 deletions(-) (limited to 'src') diff --git a/src/luarocks/fetch.lua b/src/luarocks/fetch.lua index 25add66a..ff77882e 100644 --- a/src/luarocks/fetch.lua +++ b/src/luarocks/fetch.lua @@ -30,9 +30,9 @@ function fetch_url(url, filename) if protocol == "file" then return fs.absolute_name(pathname) elseif protocol == "http" or protocol == "ftp" or protocol == "https" then - local ok = fs.download(url, filename) + local ok, err = fs.download(url, filename) if not ok then - return nil, "Failed downloading "..url, "network" + return nil, "Failed downloading "..url.." - "..err, "network" end return dir.path(fs.current_dir(), filename or dir.base_name(url)) else diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 79baa07f..8d29f109 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua @@ -457,54 +457,28 @@ end if socket_ok then - local ltn12 = require("ltn12") - ---- Download a remote file. --- @param url string: URL to be fetched. --- @param filename string or nil: this function attempts to detect the --- resulting local filename of the remote file as the basename of the URL; --- if that is not correct (due to a redirection, for example), the local --- filename can be given explicitly as this second argument. --- @return boolean: true on success, false on failure. -function download(url, filename) - assert(type(url) == "string") - assert(type(filename) == "string" or not filename) - - filename = dir.path(fs.current_dir(), filename or dir.base_name(url)) - - local content, err - if util.starts_with(url, "http:") then - local proxy = cfg.proxy - local url_arg, proxy_result - if proxy then - proxy_result = {} - url_arg = { url = url, proxy = proxy, sink = ltn12.sink.table(proxy_result) } - else - url_arg = url - end - local res, status, headers, line = http.request(url_arg) - if not res then - err = status - elseif status ~= 200 then - err = line - else - if proxy_result then res = table.concat(proxy_result) end - content = res - end - elseif util.starts_with(url, "ftp:") then - content, err = ftp.get(url) +local luasec_ok, https = pcall(require, "ssl.https") + +local function http_request(url, http) + local proxy = cfg.proxy + local url_arg, proxy_result + if proxy then + proxy_result = {} + url_arg = { url = url, proxy = proxy, sink = ltn12.sink.table(proxy_result) } else - err = "Unsupported protocol in URL: "..url + url_arg = url end - if not content then - return false, "Failed downloading: " .. err + local res, status, headers, line = http.request(url_arg) + if not res then + err = status + elseif status ~= 200 then + err = line + else + if proxy_result then res = table.concat(proxy_result) end + content = res end - local file = io.open(filename, "wb") - if not file then return false end - file:write(content) - file:close() - return true + return content, err end --- Download a remote file. @@ -519,22 +493,23 @@ function download(url, filename) assert(type(filename) == "string" or not filename) filename = dir.path(fs.current_dir(), filename or dir.base_name(url)) - + local content, err if util.starts_with(url, "http:") then - local res, status, headers, line = http.request(url) - if not res then - err = status - elseif status ~= 200 then - err = line - else - content = res - end + content, err = http_request(url, http) elseif util.starts_with(url, "ftp:") then content, err = ftp.get(url) + elseif util.starts_with(url, "https:") then + if luasec_ok then + content, err = http_request(url, https) + else + err = "Unsupported protocol - install luasec to get HTTPS support." + end + else + err = "Unsupported protocol" end if not content then - return false, "Failed downloading: " .. err + return false, err end local file = io.open(filename, "wb") if not file then return false end @@ -543,7 +518,6 @@ function download(url, filename) return true end - end --------------------------------------------------------------------- -- MD5 functions -- cgit v1.2.3-55-g6feb