aboutsummaryrefslogtreecommitdiff
path: root/etc
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2003-03-20 00:24:44 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2003-03-20 00:24:44 +0000
commit53857360bb1ca9cd2080b69d930763ae59db9b06 (patch)
tree6c1bc6d6462695cf9048801b2244f7fd0cd21ad5 /etc
parent7da19138e37c4e0123860f1fecbceb80c3d2627d (diff)
downloadluasocket-53857360bb1ca9cd2080b69d930763ae59db9b06.tar.gz
luasocket-53857360bb1ca9cd2080b69d930763ae59db9b06.tar.bz2
luasocket-53857360bb1ca9cd2080b69d930763ae59db9b06.zip
Finish port to Lua 5. Everything is working fine.
Still doesn't work in Windows.
Diffstat (limited to 'etc')
-rw-r--r--etc/check-links.lua58
-rw-r--r--etc/get.lua46
2 files changed, 50 insertions, 54 deletions
diff --git a/etc/check-links.lua b/etc/check-links.lua
index 24747a9..0dca27c 100644
--- a/etc/check-links.lua
+++ b/etc/check-links.lua
@@ -1,13 +1,9 @@
1dofile("../lua/http.lua") 1socket.http.TIMEOUT = 10
2HTTP.TIMEOUT = 10
3dofile("../lua/code.lua")
4dofile("../lua/url.lua")
5dofile("../lua/concat.lua")
6 2
7cache = {} 3cache = {}
8 4
9function readfile(path) 5function readfile(path)
10 path = Code.unescape(path) 6 path = socket.code.unescape(path)
11 local file, error = openfile(path, "r") 7 local file, error = openfile(path, "r")
12 if file then 8 if file then
13 local body = read(file, "*a") 9 local body = read(file, "*a")
@@ -17,7 +13,7 @@ function readfile(path)
17end 13end
18 14
19function getstatus(url) 15function getstatus(url)
20 local parsed = URL.parse_url(url, { scheme = "file" }) 16 local parsed = socket.url.parse(url, { scheme = "file" })
21 if cache[url] then return cache[url].res end 17 if cache[url] then return cache[url].res end
22 local res 18 local res
23 if parsed.scheme == "http" then 19 if parsed.scheme == "http" then
@@ -25,10 +21,10 @@ function getstatus(url)
25 local response = { body_cb = function(chunk, err) 21 local response = { body_cb = function(chunk, err)
26 return nil 22 return nil
27 end } 23 end }
28 local blocksize = HTTP.BLOCKSIZE 24 local blocksize = socket.http.BLOCKSIZE
29 HTTP.BLOCKSIZE = 1 25 socket.http.BLOCKSIZE = 1
30 response = HTTP.request_cb(request, response) 26 response = socket.http.request_cb(request, response)
31 HTTP.BLOCKSIZE = blocksize 27 socket.http.BLOCKSIZE = blocksize
32 if response.code == 200 then res = nil 28 if response.code == 200 then res = nil
33 else res = response.status or response.error end 29 else res = response.status or response.error end
34 elseif parsed.scheme == "file" then 30 elseif parsed.scheme == "file" then
@@ -37,17 +33,17 @@ function getstatus(url)
37 closefile(file) 33 closefile(file)
38 res = nil 34 res = nil
39 else res = error end 35 else res = error end
40 else res = format("unhandled scheme '%s'", parsed.scheme) end 36 else res = string.format("unhandled scheme '%s'", parsed.scheme) end
41 cache[url] = { res = res } 37 cache[url] = { res = res }
42 return res 38 return res
43end 39end
44 40
45function retrieve(url) 41function retrieve(url)
46 local parsed = URL.parse_url(url, { scheme = "file" }) 42 local parsed = socket.url.parse(url, { scheme = "file" })
47 local base, body, error 43 local base, body, error
48 base = url 44 base = url
49 if parsed.scheme == "http" then 45 if parsed.scheme == "http" then
50 local response = HTTP.request{url = url} 46 local response = socket.http.request{url = url}
51 if response.code ~= 200 then 47 if response.code ~= 200 then
52 error = response.status or response.error 48 error = response.status or response.error
53 else 49 else
@@ -56,23 +52,23 @@ function retrieve(url)
56 end 52 end
57 elseif parsed.scheme == "file" then 53 elseif parsed.scheme == "file" then
58 body, error = readfile(parsed.path) 54 body, error = readfile(parsed.path)
59 else error = format("unhandled scheme '%s'", parsed.scheme) end 55 else error = string.format("unhandled scheme '%s'", parsed.scheme) end
60 return base, body, error 56 return base, body, error
61end 57end
62 58
63function getlinks(body, base) 59function getlinks(body, base)
64 -- get rid of comments 60 -- get rid of comments
65 body = gsub(body, "%<%!%-%-.-%-%-%>", "") 61 body = string.gsub(body, "%<%!%-%-.-%-%-%>", "")
66 local links = {} 62 local links = {}
67 -- extract links 63 -- extract links
68 gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href) 64 string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href)
69 tinsert(%links, URL.absolute_url(%base, href)) 65 table.insert(links, socket.url.absolute(base, href))
70 end) 66 end)
71 gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href) 67 string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href)
72 tinsert(%links, URL.absolute_url(%base, href)) 68 table.insert(links, socket.url.absolute(base, href))
73 end) 69 end)
74 gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(%a+)", function(href) 70 string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(%a+)", function(href)
75 tinsert(%links, URL.absolute_url(%base, href)) 71 table.insert(links, socket.url.absolute(base, href))
76 end) 72 end)
77 return links 73 return links
78end 74end
@@ -81,19 +77,19 @@ function checklinks(url)
81 local base, body, error = retrieve(url) 77 local base, body, error = retrieve(url)
82 if not body then print(error) return end 78 if not body then print(error) return end
83 local links = getlinks(body, base) 79 local links = getlinks(body, base)
84 for i = 1, getn(links) do 80 for _, l in ipairs(links) do
85 write(_STDERR, "\t", links[i], "\n") 81 io.stderr:write("\t", l, "\n")
86 local err = getstatus(links[i]) 82 local err = getstatus(l)
87 if err then write('\t', links[i], ": ", err, "\n") end 83 if err then io.stderr:write('\t', l, ": ", err, "\n") end
88 end 84 end
89end 85end
90 86
91arg = arg or {} 87arg = arg or {}
92if getn(arg) < 1 then 88if table.getn(arg) < 1 then
93 write("Usage:\n luasocket -f check-links.lua {<url>}\n") 89 print("Usage:\n luasocket check-links.lua {<url>}")
94 exit() 90 exit()
95end 91end
96for i = 1, getn(arg) do 92for _, a in ipairs(arg) do
97 write("Checking ", arg[i], "\n") 93 print("Checking ", a)
98 checklinks(URL.absolute_url("file:", arg[i])) 94 checklinks(socket.url.absolute("file:", a))
99end 95end
diff --git a/etc/get.lua b/etc/get.lua
index ecfecd1..33da653 100644
--- a/etc/get.lua
+++ b/etc/get.lua
@@ -13,8 +13,8 @@ function nicetime(s)
13 end 13 end
14 end 14 end
15 end 15 end
16 if l == "s" then return format("%2.0f%s", s, l) 16 if l == "s" then return string.format("%2.0f%s", s, l)
17 else return format("%5.2f%s", s, l) end 17 else return string.format("%5.2f%s", s, l) end
18end 18end
19 19
20-- formats a number of bytes into human readable form 20-- formats a number of bytes into human readable form
@@ -32,21 +32,21 @@ function nicesize(b)
32 end 32 end
33 end 33 end
34 end 34 end
35 return format("%7.2f%2s", b, l) 35 return string.format("%7.2f%2s", b, l)
36end 36end
37 37
38-- returns a string with the current state of the download 38-- returns a string with the current state of the download
39function gauge(got, dt, size) 39function gauge(got, dt, size)
40 local rate = got / dt 40 local rate = got / dt
41 if size and size >= 1 then 41 if size and size >= 1 then
42 return format("%s received, %s/s throughput, " .. 42 return string.format("%s received, %s/s throughput, " ..
43 "%.0f%% done, %s remaining", 43 "%.0f%% done, %s remaining",
44 nicesize(got), 44 nicesize(got),
45 nicesize(rate), 45 nicesize(rate),
46 100*got/size, 46 100*got/size,
47 nicetime((size-got)/rate)) 47 nicetime((size-got)/rate))
48 else 48 else
49 return format("%s received, %s/s throughput, %s elapsed", 49 return string.format("%s received, %s/s throughput, %s elapsed",
50 nicesize(got), 50 nicesize(got),
51 nicesize(rate), 51 nicesize(rate),
52 nicetime(dt)) 52 nicetime(dt))
@@ -57,22 +57,22 @@ end
57-- kind of copied from luasocket's manual callback examples 57-- kind of copied from luasocket's manual callback examples
58function receive2disk(file, size) 58function receive2disk(file, size)
59 local aux = { 59 local aux = {
60 start = _time(), 60 start = socket._time(),
61 got = 0, 61 got = 0,
62 file = openfile(file, "wb"), 62 file = io.open(file, "wb"),
63 size = size 63 size = size
64 } 64 }
65 local receive_cb = function(chunk, err) 65 local receive_cb = function(chunk, err)
66 local dt = _time() - %aux.start -- elapsed time since start 66 local dt = socket._time() - %aux.start -- elapsed time since start
67 if not chunk or chunk == "" then 67 if not chunk or chunk == "" then
68 write("\n") 68 io.write("\n")
69 closefile(%aux.file) 69 aux.file:close()
70 return 70 return
71 end 71 end
72 write(%aux.file, chunk) 72 aux.file:write(chunk)
73 %aux.got = %aux.got + strlen(chunk) -- total bytes received 73 aux.got = aux.got + string.len(chunk) -- total bytes received
74 if dt < 0.1 then return 1 end -- not enough time for estimate 74 if dt < 0.1 then return 1 end -- not enough time for estimate
75 write("\r", gauge(%aux.got, dt, %aux.size)) 75 io.write("\r", gauge(aux.got, dt, aux.size))
76 return 1 76 return 1
77 end 77 end
78 return receive_cb 78 return receive_cb
@@ -80,7 +80,7 @@ end
80 80
81-- downloads a file using the ftp protocol 81-- downloads a file using the ftp protocol
82function getbyftp(url, file) 82function getbyftp(url, file)
83 local err = FTP.get_cb { 83 local err = socket.ftp.get_cb {
84 url = url, 84 url = url,
85 content_cb = receive2disk(file), 85 content_cb = receive2disk(file),
86 type = "i" 86 type = "i"
@@ -91,7 +91,7 @@ end
91 91
92-- downloads a file using the http protocol 92-- downloads a file using the http protocol
93function getbyhttp(url, file, size) 93function getbyhttp(url, file, size)
94 local response = HTTP.request_cb( 94 local response = socket.http.request_cb(
95 {url = url}, 95 {url = url},
96 {body_cb = receive2disk(file, size)} 96 {body_cb = receive2disk(file, size)}
97 ) 97 )
@@ -101,7 +101,7 @@ end
101 101
102-- determines the size of a http file 102-- determines the size of a http file
103function gethttpsize(url) 103function gethttpsize(url)
104 local response = HTTP.request { 104 local response = socket.http.request {
105 method = "HEAD", 105 method = "HEAD",
106 url = url 106 url = url
107 } 107 }
@@ -113,11 +113,11 @@ end
113-- determines the scheme and the file name of a given url 113-- determines the scheme and the file name of a given url
114function getschemeandname(url, name) 114function getschemeandname(url, name)
115 -- this is an heuristic to solve a common invalid url poblem 115 -- this is an heuristic to solve a common invalid url poblem
116 if not strfind(url, "//") then url = "//" .. url end 116 if not string.find(url, "//") then url = "//" .. url end
117 local parsed = URL.parse_url(url, {scheme = "http"}) 117 local parsed = socket.url.parse(url, {scheme = "http"})
118 if name then return parsed.scheme, name end 118 if name then return parsed.scheme, name end
119 local segment = URL.parse_path(parsed.path) 119 local segment = socket.url.parse_path(parsed.path)
120 name = segment[getn(segment)] 120 name = segment[table.getn(segment)]
121 if segment.is_directory then name = nil end 121 if segment.is_directory then name = nil end
122 return parsed.scheme, name 122 return parsed.scheme, name
123end 123end
@@ -134,7 +134,7 @@ end
134 134
135-- main program 135-- main program
136arg = arg or {} 136arg = arg or {}
137if getn(arg) < 1 then 137if table.getn(arg) < 1 then
138 write("Usage:\n luasocket -f get.lua <remote-url> [<local-file>]\n") 138 io.write("Usage:\n luasocket get.lua <remote-url> [<local-file>]\n")
139 exit(1) 139 os.exit(1)
140else get(arg[1], arg[2]) end 140else get(arg[1], arg[2]) end