aboutsummaryrefslogtreecommitdiff
path: root/etc
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-01-19 05:41:30 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-01-19 05:41:30 +0000
commit5b8d7dec541a618b4ca7f2205470a28cde2e3e25 (patch)
tree209ad0c80c9a938068401fc5b8fa51942972418f /etc
parent6ac82d50eecdf9bf55f4234ed3a5449afd7a2992 (diff)
downloadluasocket-5b8d7dec541a618b4ca7f2205470a28cde2e3e25.tar.gz
luasocket-5b8d7dec541a618b4ca7f2205470a28cde2e3e25.tar.bz2
luasocket-5b8d7dec541a618b4ca7f2205470a28cde2e3e25.zip
Updated some of the callbacks in callback.lua.
Update get.lua to use the new callbacks. The old "code" module is now the "mime" module. Updated all modules that depended on it. Updated url.lua to use the new namespace scheme, and moved the escape and unescape functions that used to be in the code.lua module to it, since these are specific to urls. Updated the callback entries in the manual.
Diffstat (limited to 'etc')
-rw-r--r--etc/eol.lua2
-rw-r--r--etc/get.lua83
2 files changed, 42 insertions, 43 deletions
diff --git a/etc/eol.lua b/etc/eol.lua
index 234cc4d..fea5da9 100644
--- a/etc/eol.lua
+++ b/etc/eol.lua
@@ -1,7 +1,7 @@
1marker = {['-u'] = '\10', ['-d'] = '\13\10'} 1marker = {['-u'] = '\10', ['-d'] = '\13\10'}
2arg = arg or {'-u'} 2arg = arg or {'-u'}
3marker = marker[arg[1]] or marker['-u'] 3marker = marker[arg[1]] or marker['-u']
4local convert = socket.code.canonic(marker) 4local convert = socket.mime.canonic(marker)
5while 1 do 5while 1 do
6 local chunk = io.read(4096) 6 local chunk = io.read(4096)
7 io.write(convert(chunk)) 7 io.write(convert(chunk))
diff --git a/etc/get.lua b/etc/get.lua
index 2d804a0..9f29a51 100644
--- a/etc/get.lua
+++ b/etc/get.lua
@@ -42,8 +42,8 @@ function nicesize(b)
42end 42end
43 43
44-- returns a string with the current state of the download 44-- returns a string with the current state of the download
45function gauge(got, dt, size) 45function gauge(got, delta, size)
46 local rate = got / dt 46 local rate = got / delta
47 if size and size >= 1 then 47 if size and size >= 1 then
48 return string.format("%s received, %s/s throughput, " .. 48 return string.format("%s received, %s/s throughput, " ..
49 "%.0f%% done, %s remaining", 49 "%.0f%% done, %s remaining",
@@ -55,53 +55,56 @@ function gauge(got, dt, size)
55 return string.format("%s received, %s/s throughput, %s elapsed", 55 return string.format("%s received, %s/s throughput, %s elapsed",
56 nicesize(got), 56 nicesize(got),
57 nicesize(rate), 57 nicesize(rate),
58 nicetime(dt)) 58 nicetime(delta))
59 end 59 end
60end 60end
61 61
62-- creates a new instance of a receive_cb that saves to disk 62-- creates a new instance of a receive_cb that saves to disk
63-- kind of copied from luasocket's manual callback examples 63-- kind of copied from luasocket's manual callback examples
64function receive2disk(file, size) 64function stats(size)
65 local aux = { 65 local start = socket.time()
66 start = socket.time(), 66 local got = 0
67 got = 0, 67 return function(chunk)
68 file = io.open(file, "wb"), 68 -- elapsed time since start
69 size = size 69 local delta = socket.time() - start
70 } 70 if chunk then
71 local receive_cb = function(chunk, err) 71 -- total bytes received
72 local dt = socket.time() - aux.start -- elapsed time since start 72 got = got + string.len(chunk)
73 if not chunk or chunk == "" then 73 -- not enough time for estimate
74 io.write("\n") 74 if delta > 0.1 then
75 aux.file:close() 75 io.stderr:write("\r", gauge(got, delta, size))
76 return 76 io.stderr:flush()
77 end
78 return chunk
79 else
80 -- close up
81 io.stderr:write("\n")
82 return ""
77 end 83 end
78 aux.file:write(chunk)
79 aux.got = aux.got + string.len(chunk) -- total bytes received
80 if dt < 0.1 then return 1 end -- not enough time for estimate
81 io.write("\r", gauge(aux.got, dt, aux.size))
82 return 1
83 end 84 end
84 return receive_cb
85end 85end
86 86
87-- downloads a file using the ftp protocol 87-- downloads a file using the ftp protocol
88function getbyftp(url, file) 88function getbyftp(url, file)
89 local save = socket.callback.receive.file(file or io.stdout)
90 if file then
91 save = socket.callback.receive.chain(stats(gethttpsize(url)), save)
92 end
89 local err = socket.ftp.get_cb { 93 local err = socket.ftp.get_cb {
90 url = url, 94 url = url,
91 content_cb = receive2disk(file), 95 content_cb = save,
92 type = "i" 96 type = "i"
93 } 97 }
94 print()
95 if err then print(err) end 98 if err then print(err) end
96end 99end
97 100
98-- downloads a file using the http protocol 101-- downloads a file using the http protocol
99function getbyhttp(url, file, size) 102function getbyhttp(url, file)
100 local response = socket.http.request_cb( 103 local save = socket.callback.receive.file(file or io.stdout)
101 {url = url}, 104 if file then
102 {body_cb = receive2disk(file, size)} 105 save = socket.callback.receive.chain(stats(gethttpsize(url)), save)
103 ) 106 end
104 print() 107 local response = socket.http.request_cb({url = url}, {body_cb = save})
105 if response.code ~= 200 then print(response.status or response.error) end 108 if response.code ~= 200 then print(response.status or response.error) end
106end 109end
107 110
@@ -116,26 +119,22 @@ function gethttpsize(url)
116 end 119 end
117end 120end
118 121
119-- determines the scheme and the file name of a given url 122-- determines the scheme
120function getschemeandname(url, name) 123function getscheme(url)
121 -- this is an heuristic to solve a common invalid url poblem 124 -- this is an heuristic to solve a common invalid url poblem
122 if not string.find(url, "//") then url = "//" .. url end 125 if not string.find(url, "//") then url = "//" .. url end
123 local parsed = socket.url.parse(url, {scheme = "http"}) 126 local parsed = socket.url.parse(url, {scheme = "http"})
124 if name then return parsed.scheme, name end 127 return parsed.scheme
125 local segment = socket.url.parse_path(parsed.path)
126 name = segment[table.getn(segment)]
127 if segment.is_directory then name = nil end
128 return parsed.scheme, name
129end 128end
130 129
131-- gets a file either by http or ftp, saving as <name> 130-- gets a file either by http or ftp, saving as <name>
132function get(url, name) 131function get(url, name)
133 local scheme 132 local fout = name and io.open(name, "wb")
134 scheme, name = getschemeandname(url, name) 133 local scheme = getscheme(url)
135 if not name then print("unknown file name") 134 if scheme == "ftp" then getbyftp(url, fout)
136 elseif scheme == "ftp" then getbyftp(url, name) 135 elseif scheme == "http" then getbyhttp(url, fout)
137 elseif scheme == "http" then getbyhttp(url, name, gethttpsize(url))
138 else print("unknown scheme" .. scheme) end 136 else print("unknown scheme" .. scheme) end
137 if name then fout:close() end
139end 138end
140 139
141-- main program 140-- main program