2003-03-28 13:08:50 -08:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Little program that checks links in HTML files
|
|
|
|
-- LuaSocket 1.5 sample files.
|
2003-06-09 11:23:40 -07:00
|
|
|
-- Author: Diego Nehab
|
|
|
|
-- RCS ID: $Id$
|
2003-03-28 13:08:50 -08:00
|
|
|
-----------------------------------------------------------------------------
|
2003-03-19 16:24:44 -08:00
|
|
|
socket.http.TIMEOUT = 10
|
2001-09-27 13:02:58 -07:00
|
|
|
|
|
|
|
cache = {}
|
|
|
|
|
|
|
|
function readfile(path)
|
2003-03-19 16:24:44 -08:00
|
|
|
path = socket.code.unescape(path)
|
2001-09-27 13:02:58 -07:00
|
|
|
local file, error = openfile(path, "r")
|
|
|
|
if file then
|
|
|
|
local body = read(file, "*a")
|
|
|
|
closefile(file)
|
|
|
|
return body
|
|
|
|
else return nil, error end
|
|
|
|
end
|
|
|
|
|
|
|
|
function getstatus(url)
|
2003-03-19 16:24:44 -08:00
|
|
|
local parsed = socket.url.parse(url, { scheme = "file" })
|
2003-03-28 13:08:50 -08:00
|
|
|
if cache[url] then return cache[url] end
|
2001-09-27 13:02:58 -07:00
|
|
|
local res
|
|
|
|
if parsed.scheme == "http" then
|
|
|
|
local request = { url = url }
|
|
|
|
local response = { body_cb = function(chunk, err)
|
|
|
|
return nil
|
|
|
|
end }
|
2003-03-19 16:24:44 -08:00
|
|
|
local blocksize = socket.http.BLOCKSIZE
|
|
|
|
socket.http.BLOCKSIZE = 1
|
|
|
|
response = socket.http.request_cb(request, response)
|
|
|
|
socket.http.BLOCKSIZE = blocksize
|
2001-09-27 13:02:58 -07:00
|
|
|
if response.code == 200 then res = nil
|
|
|
|
else res = response.status or response.error end
|
|
|
|
elseif parsed.scheme == "file" then
|
|
|
|
local file, error = openfile(Code.unescape(parsed.path), "r")
|
|
|
|
if file then
|
|
|
|
closefile(file)
|
|
|
|
res = nil
|
|
|
|
else res = error end
|
2003-03-19 16:24:44 -08:00
|
|
|
else res = string.format("unhandled scheme '%s'", parsed.scheme) end
|
2003-03-28 13:08:50 -08:00
|
|
|
cache[url] = res
|
2001-09-27 13:02:58 -07:00
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
function retrieve(url)
|
2003-03-19 16:24:44 -08:00
|
|
|
local parsed = socket.url.parse(url, { scheme = "file" })
|
2001-09-27 13:02:58 -07:00
|
|
|
local base, body, error
|
|
|
|
base = url
|
|
|
|
if parsed.scheme == "http" then
|
2003-03-19 16:24:44 -08:00
|
|
|
local response = socket.http.request{url = url}
|
2001-09-27 13:02:58 -07:00
|
|
|
if response.code ~= 200 then
|
|
|
|
error = response.status or response.error
|
|
|
|
else
|
|
|
|
base = response.headers.location or url
|
|
|
|
body = response.body
|
|
|
|
end
|
|
|
|
elseif parsed.scheme == "file" then
|
|
|
|
body, error = readfile(parsed.path)
|
2003-03-19 16:24:44 -08:00
|
|
|
else error = string.format("unhandled scheme '%s'", parsed.scheme) end
|
2001-09-27 13:02:58 -07:00
|
|
|
return base, body, error
|
|
|
|
end
|
|
|
|
|
|
|
|
function getlinks(body, base)
|
|
|
|
-- get rid of comments
|
2003-03-19 16:24:44 -08:00
|
|
|
body = string.gsub(body, "%<%!%-%-.-%-%-%>", "")
|
2001-09-27 13:02:58 -07:00
|
|
|
local links = {}
|
|
|
|
-- extract links
|
2003-03-19 16:24:44 -08:00
|
|
|
string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href)
|
|
|
|
table.insert(links, socket.url.absolute(base, href))
|
2001-09-27 13:02:58 -07:00
|
|
|
end)
|
2003-03-19 16:24:44 -08:00
|
|
|
string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href)
|
|
|
|
table.insert(links, socket.url.absolute(base, href))
|
2001-09-27 13:02:58 -07:00
|
|
|
end)
|
2003-03-19 16:24:44 -08:00
|
|
|
string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(%a+)", function(href)
|
|
|
|
table.insert(links, socket.url.absolute(base, href))
|
2001-09-27 13:02:58 -07:00
|
|
|
end)
|
|
|
|
return links
|
|
|
|
end
|
|
|
|
|
|
|
|
function checklinks(url)
|
|
|
|
local base, body, error = retrieve(url)
|
|
|
|
if not body then print(error) return end
|
|
|
|
local links = getlinks(body, base)
|
2003-03-19 16:24:44 -08:00
|
|
|
for _, l in ipairs(links) do
|
|
|
|
io.stderr:write("\t", l, "\n")
|
|
|
|
local err = getstatus(l)
|
|
|
|
if err then io.stderr:write('\t', l, ": ", err, "\n") end
|
2001-09-27 13:02:58 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
arg = arg or {}
|
2003-03-19 16:24:44 -08:00
|
|
|
if table.getn(arg) < 1 then
|
|
|
|
print("Usage:\n luasocket check-links.lua {<url>}")
|
2001-09-27 13:02:58 -07:00
|
|
|
exit()
|
|
|
|
end
|
2003-03-19 16:24:44 -08:00
|
|
|
for _, a in ipairs(arg) do
|
|
|
|
print("Checking ", a)
|
|
|
|
checklinks(socket.url.absolute("file:", a))
|
2001-09-27 13:02:58 -07:00
|
|
|
end
|