diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-06-06 20:59:36 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2001-06-06 20:59:36 +0000 |
commit | 8f1349ddd4b0af71bec244a1be09b09c22709ae1 (patch) | |
tree | 5a84737302c3304130732617beb968cff746b596 /etc | |
parent | f66963f9a199250dc836835db649679e4dce881f (diff) | |
download | luasocket-8f1349ddd4b0af71bec244a1be09b09c22709ae1.tar.gz luasocket-8f1349ddd4b0af71bec244a1be09b09c22709ae1.tar.bz2 luasocket-8f1349ddd4b0af71bec244a1be09b09c22709ae1.zip |
Initial revision
Diffstat (limited to 'etc')
-rw-r--r-- | etc/get.lua | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/etc/get.lua b/etc/get.lua new file mode 100644 index 0000000..4a17cfc --- /dev/null +++ b/etc/get.lua | |||
@@ -0,0 +1,100 @@ | |||
1 | assert(dofile("../lua/buffer.lua")) | ||
2 | assert(dofile("../lua/ftp.lua")) | ||
3 | assert(dofile("../lua/base64.lua")) | ||
4 | assert(dofile("../lua/http.lua")) | ||
5 | |||
6 | -- format a number of bytes per second into a human readable form | ||
7 | function strbps(b) | ||
8 | local l = "B/s" | ||
9 | if b > 1024 then | ||
10 | b = b / 1024 | ||
11 | l = "KB/s" | ||
12 | if b > 1024 then | ||
13 | b = b / 1024 | ||
14 | l = "MB/s" | ||
15 | if b > 1024 then | ||
16 | b = b / 1024 | ||
17 | l = "GB/s" -- hmmm | ||
18 | end | ||
19 | end | ||
20 | end | ||
21 | return format("%.2f%s ", b, l) | ||
22 | end | ||
23 | |||
24 | -- creates a new instance of a receive_cb that saves to disk | ||
25 | -- kind of copied from luasocket's manual callback examples | ||
26 | function receive2disk(file) | ||
27 | local aux = { | ||
28 | start = _time(), | ||
29 | got = 0, | ||
30 | file = openfile(file, "wb") | ||
31 | } | ||
32 | local receive_cb = function(chunk, err) | ||
33 | local dt = _time() - %aux.start -- elapsed time since start | ||
34 | if not chunk or chunk == "" then | ||
35 | write("\n") | ||
36 | closefile(%aux.file) | ||
37 | return | ||
38 | end | ||
39 | write(%aux.file, chunk) | ||
40 | %aux.got = %aux.got + strlen(chunk) -- total bytes received | ||
41 | if dt < 0.1 then return 1 end -- not enough time for estimate | ||
42 | local rate = %aux.got / dt -- get download rate | ||
43 | write("\r" .. strbps(rate)) -- print estimate | ||
44 | return 1 | ||
45 | end | ||
46 | return receive_cb | ||
47 | end | ||
48 | |||
49 | -- stolen from http implementation | ||
50 | function split_url(url, default) | ||
51 | -- initialize default parameters | ||
52 | local parsed = default or {} | ||
53 | -- get scheme | ||
54 | url = gsub(url, "^(.+)://", function (s) %parsed.scheme = s end) | ||
55 | -- get user name and password. both can be empty! | ||
56 | -- moreover, password can be ommited | ||
57 | url = gsub(url, "^([^@:/]*)(:?)([^:@/]-)@", function (u, c, p) | ||
58 | %parsed.user = u | ||
59 | -- there can be an empty password, but the ':' has to be there | ||
60 | -- or else there is no password | ||
61 | %parsed.pass = nil -- kill default password | ||
62 | if c == ":" then %parsed.pass = p end | ||
63 | end) | ||
64 | -- get host | ||
65 | url = gsub(url, "^([%w%.%-]+)", function (h) %parsed.host = h end) | ||
66 | -- get port if any | ||
67 | url = gsub(url, "^:(%d+)", function (p) %parsed.port = p end) | ||
68 | -- whatever is left is the path | ||
69 | if url ~= "" then parsed.path = url end | ||
70 | return parsed | ||
71 | end | ||
72 | |||
73 | -- stolen from http implementation | ||
74 | function get_statuscode(line) | ||
75 | local _,_, code = strfind(line, " (%d%d%d) ") | ||
76 | return tonumber(code) | ||
77 | end | ||
78 | |||
79 | function getbyftp(url, file) | ||
80 | local err = ftp_getindirect(url, receive2disk(file), "b") | ||
81 | if err then print(err) else print("done.") end | ||
82 | end | ||
83 | |||
84 | function getbyhttp(url, file) | ||
85 | local hdrs, line, err = http_getindirect(url, receive2disk(file)) | ||
86 | if line and get_statuscode(line) == 200 then print("done.") | ||
87 | elseif line then print(line) else print(err) end | ||
88 | end | ||
89 | |||
90 | function get(url, file) | ||
91 | local parsed = split_url(url) | ||
92 | if parsed.scheme == "ftp" then getbyftp(url, file) | ||
93 | else getbyhttp(url, file) end | ||
94 | end | ||
95 | |||
96 | arg = arg or {} | ||
97 | if getn(arg) < 2 then | ||
98 | write("Usage:\n luasocket -f get.lua <remote-url> <local-file>\n") | ||
99 | exit(1) | ||
100 | else get(arg[1], arg[2]) end | ||