diff options
-rw-r--r-- | src/luarocks/fs/lua.lua | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 0e11520b..f3d75b44 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua | |||
@@ -496,6 +496,54 @@ end | |||
496 | 496 | ||
497 | if socket_ok then | 497 | if socket_ok then |
498 | 498 | ||
499 | |||
500 | local ltn12 = require("ltn12") | ||
501 | |||
502 | --- Download a remote file. | ||
503 | -- @param url string: URL to be fetched. | ||
504 | -- @param filename string or nil: this function attempts to detect the | ||
505 | -- resulting local filename of the remote file as the basename of the URL; | ||
506 | -- if that is not correct (due to a redirection, for example), the local | ||
507 | -- filename can be given explicitly as this second argument. | ||
508 | -- @return boolean: true on success, false on failure. | ||
509 | function download(url, filename) | ||
510 | assert(type(url) == "string") | ||
511 | assert(type(filename) == "string" or not filename) | ||
512 | |||
513 | filename = dir.path(fs.current_dir(), filename or dir.base_name(url)) | ||
514 | |||
515 | local content, err | ||
516 | if util.starts_with(url, "http:") then | ||
517 | local proxy = cfg.proxy | ||
518 | local url_arg, proxy_result | ||
519 | if proxy then | ||
520 | proxy_result = {} | ||
521 | url_arg = { url = url, proxy = proxy, sink = ltn12.sink.table(proxy_result) } | ||
522 | else | ||
523 | url_arg = url | ||
524 | end | ||
525 | local res, status, headers, line = http.request(url_arg) | ||
526 | if not res then | ||
527 | err = status | ||
528 | elseif status ~= 200 then | ||
529 | err = line | ||
530 | else | ||
531 | if proxy_result then res = table.concat(proxy_result) end | ||
532 | content = res | ||
533 | end | ||
534 | elseif util.starts_with(url, "ftp:") then | ||
535 | content, err = ftp.get(url) | ||
536 | end | ||
537 | if not content then | ||
538 | return false, "Failed downloading: " .. err | ||
539 | end | ||
540 | local file = io.open(filename, "wb") | ||
541 | if not file then return false end | ||
542 | file:write(content) | ||
543 | file:close() | ||
544 | return true | ||
545 | end | ||
546 | |||
499 | --- Download a remote file. | 547 | --- Download a remote file. |
500 | -- @param url string: URL to be fetched. | 548 | -- @param url string: URL to be fetched. |
501 | -- @param filename string or nil: this function attempts to detect the | 549 | -- @param filename string or nil: this function attempts to detect the |
@@ -532,8 +580,8 @@ function download(url, filename) | |||
532 | return true | 580 | return true |
533 | end | 581 | end |
534 | 582 | ||
535 | end | ||
536 | 583 | ||
584 | end | ||
537 | --------------------------------------------------------------------- | 585 | --------------------------------------------------------------------- |
538 | -- MD5 functions | 586 | -- MD5 functions |
539 | --------------------------------------------------------------------- | 587 | --------------------------------------------------------------------- |