aboutsummaryrefslogtreecommitdiff
path: root/etc/check-links.lua
diff options
context:
space:
mode:
Diffstat (limited to 'etc/check-links.lua')
-rw-r--r--etc/check-links.lua58
1 files changed, 27 insertions, 31 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