diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-03-16 06:42:53 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-03-16 06:42:53 +0000 |
commit | bcc0c2a9f0be2ca796ef5206a78e283fe15e6186 (patch) | |
tree | 65c269d4854aa5ff4a0b2c8eede5cdb18d716033 /etc | |
parent | b6edaac2841137cf0ef5105f75358bbab4570d87 (diff) | |
download | luasocket-bcc0c2a9f0be2ca796ef5206a78e283fe15e6186.tar.gz luasocket-bcc0c2a9f0be2ca796ef5206a78e283fe15e6186.tar.bz2 luasocket-bcc0c2a9f0be2ca796ef5206a78e283fe15e6186.zip |
New filter scheme.
ltn12 and mime updated.
smtp/ftp broken.
Diffstat (limited to 'etc')
-rw-r--r-- | etc/b64.lua | 17 | ||||
-rw-r--r-- | etc/get.lua | 47 |
2 files changed, 27 insertions, 37 deletions
diff --git a/etc/b64.lua b/etc/b64.lua index de83578..ea157c4 100644 --- a/etc/b64.lua +++ b/etc/b64.lua | |||
@@ -1,13 +1,12 @@ | |||
1 | local source = ltn12.source.file(io.stdin) | ||
2 | local sink = ltn12.sink.file(io.stdout) | ||
1 | local convert | 3 | local convert |
2 | if arg and arg[1] == '-d' then | 4 | if arg and arg[1] == '-d' then |
3 | convert = socket.mime.decode("base64") | 5 | convert = mime.decode("base64") |
4 | else | 6 | else |
5 | local base64 = socket.mime.encode("base64") | 7 | local base64 = mime.encode("base64") |
6 | local wrap = socket.mime.wrap() | 8 | local wrap = mime.wrap() |
7 | convert = socket.mime.chain(base64, wrap) | 9 | convert = ltn12.filter.chain(base64, wrap) |
8 | end | ||
9 | while 1 do | ||
10 | local chunk = io.read(4096) | ||
11 | io.write(convert(chunk)) | ||
12 | if not chunk then break end | ||
13 | end | 10 | end |
11 | source = ltn12.source.chain(source, convert) | ||
12 | ltn12.pump(source, sink) | ||
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 |