Finish port to Lua 5. Everything is working fine.

Still doesn't work in Windows.
This commit is contained in:
Diego Nehab 2003-03-20 00:24:44 +00:00
parent 7da19138e3
commit 53857360bb
11 changed files with 289 additions and 277 deletions

View file

@ -1,13 +1,9 @@
dofile("../lua/http.lua")
HTTP.TIMEOUT = 10
dofile("../lua/code.lua")
dofile("../lua/url.lua")
dofile("../lua/concat.lua")
socket.http.TIMEOUT = 10
cache = {}
function readfile(path)
path = Code.unescape(path)
path = socket.code.unescape(path)
local file, error = openfile(path, "r")
if file then
local body = read(file, "*a")
@ -17,7 +13,7 @@ function readfile(path)
end
function getstatus(url)
local parsed = URL.parse_url(url, { scheme = "file" })
local parsed = socket.url.parse(url, { scheme = "file" })
if cache[url] then return cache[url].res end
local res
if parsed.scheme == "http" then
@ -25,10 +21,10 @@ function getstatus(url)
local response = { body_cb = function(chunk, err)
return nil
end }
local blocksize = HTTP.BLOCKSIZE
HTTP.BLOCKSIZE = 1
response = HTTP.request_cb(request, response)
HTTP.BLOCKSIZE = blocksize
local blocksize = socket.http.BLOCKSIZE
socket.http.BLOCKSIZE = 1
response = socket.http.request_cb(request, response)
socket.http.BLOCKSIZE = blocksize
if response.code == 200 then res = nil
else res = response.status or response.error end
elseif parsed.scheme == "file" then
@ -37,17 +33,17 @@ function getstatus(url)
closefile(file)
res = nil
else res = error end
else res = format("unhandled scheme '%s'", parsed.scheme) end
else res = string.format("unhandled scheme '%s'", parsed.scheme) end
cache[url] = { res = res }
return res
end
function retrieve(url)
local parsed = URL.parse_url(url, { scheme = "file" })
local parsed = socket.url.parse(url, { scheme = "file" })
local base, body, error
base = url
if parsed.scheme == "http" then
local response = HTTP.request{url = url}
local response = socket.http.request{url = url}
if response.code ~= 200 then
error = response.status or response.error
else
@ -56,23 +52,23 @@ function retrieve(url)
end
elseif parsed.scheme == "file" then
body, error = readfile(parsed.path)
else error = format("unhandled scheme '%s'", parsed.scheme) end
else error = string.format("unhandled scheme '%s'", parsed.scheme) end
return base, body, error
end
function getlinks(body, base)
-- get rid of comments
body = gsub(body, "%<%!%-%-.-%-%-%>", "")
body = string.gsub(body, "%<%!%-%-.-%-%-%>", "")
local links = {}
-- extract links
gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href)
tinsert(%links, URL.absolute_url(%base, href))
string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href)
table.insert(links, socket.url.absolute(base, href))
end)
gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href)
tinsert(%links, URL.absolute_url(%base, href))
string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href)
table.insert(links, socket.url.absolute(base, href))
end)
gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(%a+)", function(href)
tinsert(%links, URL.absolute_url(%base, href))
string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(%a+)", function(href)
table.insert(links, socket.url.absolute(base, href))
end)
return links
end
@ -81,19 +77,19 @@ function checklinks(url)
local base, body, error = retrieve(url)
if not body then print(error) return end
local links = getlinks(body, base)
for i = 1, getn(links) do
write(_STDERR, "\t", links[i], "\n")
local err = getstatus(links[i])
if err then write('\t', links[i], ": ", err, "\n") end
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
end
end
arg = arg or {}
if getn(arg) < 1 then
write("Usage:\n luasocket -f check-links.lua {<url>}\n")
if table.getn(arg) < 1 then
print("Usage:\n luasocket check-links.lua {<url>}")
exit()
end
for i = 1, getn(arg) do
write("Checking ", arg[i], "\n")
checklinks(URL.absolute_url("file:", arg[i]))
for _, a in ipairs(arg) do
print("Checking ", a)
checklinks(socket.url.absolute("file:", a))
end