diff options
Diffstat (limited to 'etc/check-links.lua')
-rw-r--r-- | etc/check-links.lua | 41 |
1 files changed, 16 insertions, 25 deletions
diff --git a/etc/check-links.lua b/etc/check-links.lua index c45131c..03ca6de 100644 --- a/etc/check-links.lua +++ b/etc/check-links.lua | |||
@@ -9,11 +9,11 @@ socket.http.TIMEOUT = 10 | |||
9 | cache = {} | 9 | cache = {} |
10 | 10 | ||
11 | function readfile(path) | 11 | function readfile(path) |
12 | path = socket.code.unescape(path) | 12 | path = socket.url.unescape(path) |
13 | local file, error = openfile(path, "r") | 13 | local file, error = io.open(path, "r") |
14 | if file then | 14 | if file then |
15 | local body = read(file, "*a") | 15 | local body = file:read("*a") |
16 | closefile(file) | 16 | file:close() |
17 | return body | 17 | return body |
18 | else return nil, error end | 18 | else return nil, error end |
19 | end | 19 | end |
@@ -23,20 +23,14 @@ function getstatus(url) | |||
23 | if cache[url] then return cache[url] end | 23 | if cache[url] then return cache[url] end |
24 | local res | 24 | local res |
25 | if parsed.scheme == "http" then | 25 | if parsed.scheme == "http" then |
26 | local request = { url = url } | 26 | local request = { url = url, method = "HEAD" } |
27 | local response = { body_cb = function(chunk, err) | 27 | local response = socket.http.request(request) |
28 | return nil | ||
29 | end } | ||
30 | local blocksize = socket.http.BLOCKSIZE | ||
31 | socket.http.BLOCKSIZE = 1 | ||
32 | response = socket.http.request_cb(request, response) | ||
33 | socket.http.BLOCKSIZE = blocksize | ||
34 | if response.code == 200 then res = nil | 28 | if response.code == 200 then res = nil |
35 | else res = response.status or response.error end | 29 | else res = response.status or response.error end |
36 | elseif parsed.scheme == "file" then | 30 | elseif parsed.scheme == "file" then |
37 | local file, error = openfile(Code.unescape(parsed.path), "r") | 31 | local file, error = io.open(socket.url.unescape(parsed.path), "r") |
38 | if file then | 32 | if file then |
39 | closefile(file) | 33 | file:close() |
40 | res = nil | 34 | res = nil |
41 | else res = error end | 35 | else res = error end |
42 | else res = string.format("unhandled scheme '%s'", parsed.scheme) end | 36 | else res = string.format("unhandled scheme '%s'", parsed.scheme) end |
@@ -46,15 +40,12 @@ end | |||
46 | 40 | ||
47 | function retrieve(url) | 41 | function retrieve(url) |
48 | local parsed = socket.url.parse(url, { scheme = "file" }) | 42 | local parsed = socket.url.parse(url, { scheme = "file" }) |
49 | local base, body, error | 43 | local body, headers, code, error |
50 | base = url | 44 | local base = url |
51 | if parsed.scheme == "http" then | 45 | if parsed.scheme == "http" then |
52 | local response = socket.http.request{url = url} | 46 | body, headers, code, error = socket.http.get(url) |
53 | if response.code ~= 200 then | 47 | if code == 200 then |
54 | error = response.status or response.error | 48 | base = base or headers.location |
55 | else | ||
56 | base = response.headers.location or url | ||
57 | body = response.body | ||
58 | end | 49 | end |
59 | elseif parsed.scheme == "file" then | 50 | elseif parsed.scheme == "file" then |
60 | body, error = readfile(parsed.path) | 51 | body, error = readfile(parsed.path) |
@@ -67,13 +58,13 @@ function getlinks(body, base) | |||
67 | body = string.gsub(body, "%<%!%-%-.-%-%-%>", "") | 58 | body = string.gsub(body, "%<%!%-%-.-%-%-%>", "") |
68 | local links = {} | 59 | local links = {} |
69 | -- extract links | 60 | -- extract links |
70 | string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href) | 61 | body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href) |
71 | table.insert(links, socket.url.absolute(base, href)) | 62 | table.insert(links, socket.url.absolute(base, href)) |
72 | end) | 63 | end) |
73 | string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href) | 64 | body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href) |
74 | table.insert(links, socket.url.absolute(base, href)) | 65 | table.insert(links, socket.url.absolute(base, href)) |
75 | end) | 66 | end) |
76 | string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(%a+)", function(href) | 67 | string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href) |
77 | table.insert(links, socket.url.absolute(base, href)) | 68 | table.insert(links, socket.url.absolute(base, href)) |
78 | end) | 69 | end) |
79 | return links | 70 | return links |