aboutsummaryrefslogtreecommitdiff
path: root/etc/get.lua
diff options
context:
space:
mode:
Diffstat (limited to 'etc/get.lua')
-rw-r--r--etc/get.lua39
1 files changed, 20 insertions, 19 deletions
diff --git a/etc/get.lua b/etc/get.lua
index 35de7d7..c1e0542 100644
--- a/etc/get.lua
+++ b/etc/get.lua
@@ -4,9 +4,10 @@
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id$
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7require"http" 7socket = require("socket")
8require"ftp" 8http = require("http")
9require"url" 9ftp = require("ftp")
10url = require("url")
10 11
11-- formats a number of seconds into human readable form 12-- formats a number of seconds into human readable form
12function nicetime(s) 13function nicetime(s)
@@ -84,55 +85,55 @@ function stats(size)
84end 85end
85 86
86-- determines the size of a http file 87-- determines the size of a http file
87function gethttpsize(url) 88function gethttpsize(u)
88 local respt = socket.http.request {method = "HEAD", url = url} 89 local respt = http.request {method = "HEAD", url = u}
89 if respt.code == 200 then 90 if respt.code == 200 then
90 return tonumber(respt.headers["content-length"]) 91 return tonumber(respt.headers["content-length"])
91 end 92 end
92end 93end
93 94
94-- downloads a file using the http protocol 95-- downloads a file using the http protocol
95function getbyhttp(url, file) 96function getbyhttp(u, file)
96 local save = ltn12.sink.file(file or io.stdout) 97 local save = ltn12.sink.file(file or io.stdout)
97 -- only print feedback if output is not stdout 98 -- only print feedback if output is not stdout
98 if file then save = ltn12.sink.chain(stats(gethttpsize(url)), save) end 99 if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end
99 local respt = socket.http.request {url = url, sink = save } 100 local respt = http.request {url = u, sink = save }
100 if respt.code ~= 200 then print(respt.status or respt.error) end 101 if respt.code ~= 200 then print(respt.status or respt.error) end
101end 102end
102 103
103-- downloads a file using the ftp protocol 104-- downloads a file using the ftp protocol
104function getbyftp(url, file) 105function getbyftp(u, file)
105 local save = ltn12.sink.file(file or io.stdout) 106 local save = ltn12.sink.file(file or io.stdout)
106 -- only print feedback if output is not stdout 107 -- only print feedback if output is not stdout
107 -- and we don't know how big the file is 108 -- and we don't know how big the file is
108 if file then save = ltn12.sink.chain(stats(), save) end 109 if file then save = ltn12.sink.chain(stats(), save) end
109 local gett = socket.url.parse(url) 110 local gett = url.parse(u)
110 gett.sink = save 111 gett.sink = save
111 gett.type = "i" 112 gett.type = "i"
112 local ret, err = socket.ftp.get(gett) 113 local ret, err = ftp.get(gett)
113 if err then print(err) end 114 if err then print(err) end
114end 115end
115 116
116-- determines the scheme 117-- determines the scheme
117function getscheme(url) 118function getscheme(u)
118 -- this is an heuristic to solve a common invalid url poblem 119 -- this is an heuristic to solve a common invalid url poblem
119 if not string.find(url, "//") then url = "//" .. url end 120 if not string.find(u, "//") then u = "//" .. u end
120 local parsed = socket.url.parse(url, {scheme = "http"}) 121 local parsed = url.parse(u, {scheme = "http"})
121 return parsed.scheme 122 return parsed.scheme
122end 123end
123 124
124-- gets a file either by http or ftp, saving as <name> 125-- gets a file either by http or ftp, saving as <name>
125function get(url, name) 126function get(u, name)
126 local fout = name and io.open(name, "wb") 127 local fout = name and io.open(name, "wb")
127 local scheme = getscheme(url) 128 local scheme = getscheme(u)
128 if scheme == "ftp" then getbyftp(url, fout) 129 if scheme == "ftp" then getbyftp(u, fout)
129 elseif scheme == "http" then getbyhttp(url, fout) 130 elseif scheme == "http" then getbyhttp(u, fout)
130 else print("unknown scheme" .. scheme) end 131 else print("unknown scheme" .. scheme) end
131end 132end
132 133
133-- main program 134-- main program
134arg = arg or {} 135arg = arg or {}
135if table.getn(arg) < 1 then 136if table.getn(arg) < 1 then
136 io.write("Usage:\n luasocket get.lua <remote-url> [<local-file>]\n") 137 io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n")
137 os.exit(1) 138 os.exit(1)
138else get(arg[1], arg[2]) end 139else get(arg[1], arg[2]) end