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.lua43
1 files changed, 21 insertions, 22 deletions
diff --git a/etc/check-links.lua b/etc/check-links.lua
index 898ed4b..a4e9ef8 100644
--- a/etc/check-links.lua
+++ b/etc/check-links.lua
@@ -4,15 +4,14 @@
4-- Author: Diego Nehab 4-- Author: Diego Nehab
5-- RCS ID: $Id$ 5-- RCS ID: $Id$
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7 7local http = require("http")
8require"http" 8local url = require("url")
9 9http.TIMEOUT = 10
10socket.http.TIMEOUT = 10
11 10
12cache = {} 11cache = {}
13 12
14function readfile(path) 13function readfile(path)
15 path = socket.url.unescape(path) 14 path = url.unescape(path)
16 local file, error = io.open(path, "r") 15 local file, error = io.open(path, "r")
17 if file then 16 if file then
18 local body = file:read("*a") 17 local body = file:read("*a")
@@ -21,32 +20,32 @@ function readfile(path)
21 else return nil, error end 20 else return nil, error end
22end 21end
23 22
24function getstatus(url) 23function getstatus(u)
25 local parsed = socket.url.parse(url, { scheme = "file" }) 24 local parsed = url.parse(u, {scheme = "file"})
26 if cache[url] then return cache[url] end 25 if cache[u] then return cache[u] end
27 local res 26 local res
28 if parsed.scheme == "http" then 27 if parsed.scheme == "http" then
29 local request = { url = url, method = "HEAD" } 28 local request = {url = u, method = "HEAD"}
30 local response = socket.http.request(request) 29 local response = http.request(request)
31 if response.code == 200 then res = nil 30 if response.code == 200 then res = nil
32 else res = response.status or response.error end 31 else res = response.status or response.error end
33 elseif parsed.scheme == "file" then 32 elseif parsed.scheme == "file" then
34 local file, error = io.open(socket.url.unescape(parsed.path), "r") 33 local file, error = io.open(url.unescape(parsed.path), "r")
35 if file then 34 if file then
36 file:close() 35 file:close()
37 res = nil 36 res = nil
38 else res = error end 37 else res = error end
39 else res = string.format("unhandled scheme '%s'", parsed.scheme) end 38 else res = string.format("unhandled scheme '%s'", parsed.scheme) end
40 cache[url] = res 39 cache[u] = res
41 return res 40 return res
42end 41end
43 42
44function retrieve(url) 43function retrieve(u)
45 local parsed = socket.url.parse(url, { scheme = "file" }) 44 local parsed = url.parse(u, { scheme = "file" })
46 local body, headers, code, error 45 local body, headers, code, error
47 local base = url 46 local base = u
48 if parsed.scheme == "http" then 47 if parsed.scheme == "http" then
49 body, headers, code, error = socket.http.get(url) 48 body, headers, code, error = http.get(u)
50 if code == 200 then 49 if code == 200 then
51 base = base or headers.location 50 base = base or headers.location
52 end 51 end
@@ -62,19 +61,19 @@ function getlinks(body, base)
62 local links = {} 61 local links = {}
63 -- extract links 62 -- extract links
64 body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href) 63 body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href)
65 table.insert(links, socket.url.absolute(base, href)) 64 table.insert(links, url.absolute(base, href))
66 end) 65 end)
67 body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href) 66 body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href)
68 table.insert(links, socket.url.absolute(base, href)) 67 table.insert(links, url.absolute(base, href))
69 end) 68 end)
70 string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href) 69 string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href)
71 table.insert(links, socket.url.absolute(base, href)) 70 table.insert(links, url.absolute(base, href))
72 end) 71 end)
73 return links 72 return links
74end 73end
75 74
76function checklinks(url) 75function checklinks(u)
77 local base, body, error = retrieve(url) 76 local base, body, error = retrieve(u)
78 if not body then print(error) return end 77 if not body then print(error) return end
79 local links = getlinks(body, base) 78 local links = getlinks(body, base)
80 for _, l in ipairs(links) do 79 for _, l in ipairs(links) do
@@ -91,5 +90,5 @@ if table.getn(arg) < 1 then
91end 90end
92for _, a in ipairs(arg) do 91for _, a in ipairs(arg) do
93 print("Checking ", a) 92 print("Checking ", a)
94 checklinks(socket.url.absolute("file:", a)) 93 checklinks(url.absolute("file:", a))
95end 94end