diff options
Diffstat (limited to 'etc/get.lua')
-rw-r--r-- | etc/get.lua | 47 |
1 files changed, 19 insertions, 28 deletions
diff --git a/etc/get.lua b/etc/get.lua index d6760b8..0306b54 100644 --- a/etc/get.lua +++ b/etc/get.lua | |||
@@ -80,39 +80,31 @@ function stats(size) | |||
80 | end | 80 | end |
81 | end | 81 | end |
82 | 82 | ||
83 | -- downloads a file using the ftp protocol | 83 | -- determines the size of a http file |
84 | function getbyftp(url, file) | 84 | function gethttpsize(url) |
85 | local save = socket.callback.receive.file(file or io.stdout) | 85 | local respt = socket.http.request {method = "HEAD", url = url} |
86 | if file then | 86 | if respt.code == 200 then |
87 | save = socket.callback.receive.chain(stats(gethttpsize(url)), save) | 87 | return tonumber(respt.headers["content-length"]) |
88 | end | 88 | end |
89 | local err = socket.ftp.get_cb { | ||
90 | url = url, | ||
91 | content_cb = save, | ||
92 | type = "i" | ||
93 | } | ||
94 | if err then print(err) end | ||
95 | end | 89 | end |
96 | 90 | ||
97 | -- downloads a file using the http protocol | 91 | -- downloads a file using the http protocol |
98 | function getbyhttp(url, file) | 92 | function getbyhttp(url, file) |
99 | local save = socket.callback.receive.file(file or io.stdout) | 93 | local save = ltn12.sink.file(file or io.stdout) |
100 | if file then | 94 | -- only print feedback if output is not stdout |
101 | save = socket.callback.receive.chain(stats(gethttpsize(url)), save) | 95 | if file then save = ltn12.sink.chain(stats(gethttpsize(url)), save) end |
102 | end | 96 | local respt = socket.http.request_cb({url = url, sink = save}) |
103 | local response = socket.http.request_cb({url = url}, {body_cb = save}) | 97 | if respt.code ~= 200 then print(respt.status or respt.error) end |
104 | if response.code ~= 200 then print(response.status or response.error) end | ||
105 | end | 98 | end |
106 | 99 | ||
107 | -- determines the size of a http file | 100 | -- downloads a file using the ftp protocol |
108 | function gethttpsize(url) | 101 | function getbyftp(url, file) |
109 | local response = socket.http.request { | 102 | local save = ltn12.sink.file(file or io.stdout) |
110 | method = "HEAD", | 103 | -- only print feedback if output is not stdout |
111 | url = url | 104 | -- and we don't know how big the file is |
112 | } | 105 | if file then save = ltn12.sink.chain(stats(), save) end |
113 | if response.code == 200 then | 106 | local ret, err = socket.ftp.get_cb {url = url, sink = save, type = "i"} |
114 | return tonumber(response.headers["content-length"]) | 107 | if err then print(err) end |
115 | end | ||
116 | end | 108 | end |
117 | 109 | ||
118 | -- determines the scheme | 110 | -- determines the scheme |
@@ -130,7 +122,6 @@ function get(url, name) | |||
130 | if scheme == "ftp" then getbyftp(url, fout) | 122 | if scheme == "ftp" then getbyftp(url, fout) |
131 | elseif scheme == "http" then getbyhttp(url, fout) | 123 | elseif scheme == "http" then getbyhttp(url, fout) |
132 | else print("unknown scheme" .. scheme) end | 124 | else print("unknown scheme" .. scheme) end |
133 | if name then fout:close() end | ||
134 | end | 125 | end |
135 | 126 | ||
136 | -- main program | 127 | -- main program |