diff options
| author | Hisham Muhammad <hisham@gobolinux.org> | 2010-12-21 11:34:37 -0200 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2010-12-21 11:34:37 -0200 |
| commit | 24af3a8dd304b0e56c1bfe78ba51651dba50b765 (patch) | |
| tree | eaebbd64137957dbcc019d9398435c63e7435fe4 /src | |
| parent | a0be4736ef870adc118a7d04931a9edaf7c2eda7 (diff) | |
| download | luarocks-24af3a8dd304b0e56c1bfe78ba51651dba50b765.tar.gz luarocks-24af3a8dd304b0e56c1bfe78ba51651dba50b765.tar.bz2 luarocks-24af3a8dd304b0e56c1bfe78ba51651dba50b765.zip | |
Add HTTPS support to native fs.download function, using LuaSec.
Diffstat (limited to 'src')
| -rw-r--r-- | src/luarocks/fetch.lua | 4 | ||||
| -rw-r--r-- | src/luarocks/fs/lua.lua | 84 |
2 files changed, 31 insertions, 57 deletions
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) | |||
| 30 | if protocol == "file" then | 30 | if protocol == "file" then |
| 31 | return fs.absolute_name(pathname) | 31 | return fs.absolute_name(pathname) |
| 32 | elseif protocol == "http" or protocol == "ftp" or protocol == "https" then | 32 | elseif protocol == "http" or protocol == "ftp" or protocol == "https" then |
| 33 | local ok = fs.download(url, filename) | 33 | local ok, err = fs.download(url, filename) |
| 34 | if not ok then | 34 | if not ok then |
| 35 | return nil, "Failed downloading "..url, "network" | 35 | return nil, "Failed downloading "..url.." - "..err, "network" |
| 36 | end | 36 | end |
| 37 | return dir.path(fs.current_dir(), filename or dir.base_name(url)) | 37 | return dir.path(fs.current_dir(), filename or dir.base_name(url)) |
| 38 | else | 38 | 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 | |||
| 457 | 457 | ||
| 458 | if socket_ok then | 458 | if socket_ok then |
| 459 | 459 | ||
| 460 | |||
| 461 | local ltn12 = require("ltn12") | 460 | local ltn12 = require("ltn12") |
| 462 | 461 | local luasec_ok, https = pcall(require, "ssl.https") | |
| 463 | --- Download a remote file. | 462 | |
| 464 | -- @param url string: URL to be fetched. | 463 | local function http_request(url, http) |
| 465 | -- @param filename string or nil: this function attempts to detect the | 464 | local proxy = cfg.proxy |
| 466 | -- resulting local filename of the remote file as the basename of the URL; | 465 | local url_arg, proxy_result |
| 467 | -- if that is not correct (due to a redirection, for example), the local | 466 | if proxy then |
| 468 | -- filename can be given explicitly as this second argument. | 467 | proxy_result = {} |
| 469 | -- @return boolean: true on success, false on failure. | 468 | url_arg = { url = url, proxy = proxy, sink = ltn12.sink.table(proxy_result) } |
| 470 | function download(url, filename) | ||
| 471 | assert(type(url) == "string") | ||
| 472 | assert(type(filename) == "string" or not filename) | ||
| 473 | |||
| 474 | filename = dir.path(fs.current_dir(), filename or dir.base_name(url)) | ||
| 475 | |||
| 476 | local content, err | ||
| 477 | if util.starts_with(url, "http:") then | ||
| 478 | local proxy = cfg.proxy | ||
| 479 | local url_arg, proxy_result | ||
| 480 | if proxy then | ||
| 481 | proxy_result = {} | ||
| 482 | url_arg = { url = url, proxy = proxy, sink = ltn12.sink.table(proxy_result) } | ||
| 483 | else | ||
| 484 | url_arg = url | ||
| 485 | end | ||
| 486 | local res, status, headers, line = http.request(url_arg) | ||
| 487 | if not res then | ||
| 488 | err = status | ||
| 489 | elseif status ~= 200 then | ||
| 490 | err = line | ||
| 491 | else | ||
| 492 | if proxy_result then res = table.concat(proxy_result) end | ||
| 493 | content = res | ||
| 494 | end | ||
| 495 | elseif util.starts_with(url, "ftp:") then | ||
| 496 | content, err = ftp.get(url) | ||
| 497 | else | 469 | else |
| 498 | err = "Unsupported protocol in URL: "..url | 470 | url_arg = url |
| 499 | end | 471 | end |
| 500 | if not content then | 472 | local res, status, headers, line = http.request(url_arg) |
| 501 | return false, "Failed downloading: " .. err | 473 | if not res then |
| 474 | err = status | ||
| 475 | elseif status ~= 200 then | ||
| 476 | err = line | ||
| 477 | else | ||
| 478 | if proxy_result then res = table.concat(proxy_result) end | ||
| 479 | content = res | ||
| 502 | end | 480 | end |
| 503 | local file = io.open(filename, "wb") | 481 | return content, err |
| 504 | if not file then return false end | ||
| 505 | file:write(content) | ||
| 506 | file:close() | ||
| 507 | return true | ||
| 508 | end | 482 | end |
| 509 | 483 | ||
| 510 | --- Download a remote file. | 484 | --- Download a remote file. |
| @@ -519,22 +493,23 @@ function download(url, filename) | |||
| 519 | assert(type(filename) == "string" or not filename) | 493 | assert(type(filename) == "string" or not filename) |
| 520 | 494 | ||
| 521 | filename = dir.path(fs.current_dir(), filename or dir.base_name(url)) | 495 | filename = dir.path(fs.current_dir(), filename or dir.base_name(url)) |
| 522 | 496 | ||
| 523 | local content, err | 497 | local content, err |
| 524 | if util.starts_with(url, "http:") then | 498 | if util.starts_with(url, "http:") then |
| 525 | local res, status, headers, line = http.request(url) | 499 | content, err = http_request(url, http) |
| 526 | if not res then | ||
| 527 | err = status | ||
| 528 | elseif status ~= 200 then | ||
| 529 | err = line | ||
| 530 | else | ||
| 531 | content = res | ||
| 532 | end | ||
| 533 | elseif util.starts_with(url, "ftp:") then | 500 | elseif util.starts_with(url, "ftp:") then |
| 534 | content, err = ftp.get(url) | 501 | content, err = ftp.get(url) |
| 502 | elseif util.starts_with(url, "https:") then | ||
| 503 | if luasec_ok then | ||
| 504 | content, err = http_request(url, https) | ||
| 505 | else | ||
| 506 | err = "Unsupported protocol - install luasec to get HTTPS support." | ||
| 507 | end | ||
| 508 | else | ||
| 509 | err = "Unsupported protocol" | ||
| 535 | end | 510 | end |
| 536 | if not content then | 511 | if not content then |
| 537 | return false, "Failed downloading: " .. err | 512 | return false, err |
| 538 | end | 513 | end |
| 539 | local file = io.open(filename, "wb") | 514 | local file = io.open(filename, "wb") |
| 540 | if not file then return false end | 515 | if not file then return false end |
| @@ -543,7 +518,6 @@ function download(url, filename) | |||
| 543 | return true | 518 | return true |
| 544 | end | 519 | end |
| 545 | 520 | ||
| 546 | |||
| 547 | end | 521 | end |
| 548 | --------------------------------------------------------------------- | 522 | --------------------------------------------------------------------- |
| 549 | -- MD5 functions | 523 | -- MD5 functions |
