diff options
Diffstat (limited to 'etc/check-links.lua')
-rw-r--r-- | etc/check-links.lua | 58 |
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 @@ | |||
1 | dofile("../lua/http.lua") | 1 | socket.http.TIMEOUT = 10 |
2 | HTTP.TIMEOUT = 10 | ||
3 | dofile("../lua/code.lua") | ||
4 | dofile("../lua/url.lua") | ||
5 | dofile("../lua/concat.lua") | ||
6 | 2 | ||
7 | cache = {} | 3 | cache = {} |
8 | 4 | ||
9 | function readfile(path) | 5 | function 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) | |||
17 | end | 13 | end |
18 | 14 | ||
19 | function getstatus(url) | 15 | function 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 |
43 | end | 39 | end |
44 | 40 | ||
45 | function retrieve(url) | 41 | function 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 |
61 | end | 57 | end |
62 | 58 | ||
63 | function getlinks(body, base) | 59 | function 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 |
78 | end | 74 | end |
@@ -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 |
89 | end | 85 | end |
90 | 86 | ||
91 | arg = arg or {} | 87 | arg = arg or {} |
92 | if getn(arg) < 1 then | 88 | if 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() |
95 | end | 91 | end |
96 | for i = 1, getn(arg) do | 92 | for _, 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)) |
99 | end | 95 | end |