Adjusted some of the broken examples.

master
Diego Nehab 2004-03-26 00:18:41 +00:00
parent 5b279bac9e
commit e77f179200
5 changed files with 28 additions and 35 deletions

View File

@ -9,11 +9,11 @@ socket.http.TIMEOUT = 10
cache = {}
function readfile(path)
path = socket.code.unescape(path)
local file, error = openfile(path, "r")
path = socket.url.unescape(path)
local file, error = io.open(path, "r")
if file then
local body = read(file, "*a")
closefile(file)
local body = file:read("*a")
file:close()
return body
else return nil, error end
end
@ -23,20 +23,14 @@ function getstatus(url)
if cache[url] then return cache[url] end
local res
if parsed.scheme == "http" then
local request = { url = url }
local response = { body_cb = function(chunk, err)
return nil
end }
local blocksize = socket.http.BLOCKSIZE
socket.http.BLOCKSIZE = 1
response = socket.http.request_cb(request, response)
socket.http.BLOCKSIZE = blocksize
local request = { url = url, method = "HEAD" }
local response = socket.http.request(request)
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")
local file, error = io.open(socket.url.unescape(parsed.path), "r")
if file then
closefile(file)
file:close()
res = nil
else res = error end
else res = string.format("unhandled scheme '%s'", parsed.scheme) end
@ -46,15 +40,12 @@ end
function retrieve(url)
local parsed = socket.url.parse(url, { scheme = "file" })
local base, body, error
base = url
local body, headers, code, error
local base = url
if parsed.scheme == "http" then
local response = socket.http.request{url = url}
if response.code ~= 200 then
error = response.status or response.error
else
base = response.headers.location or url
body = response.body
body, headers, code, error = socket.http.get(url)
if code == 200 then
base = base or headers.location
end
elseif parsed.scheme == "file" then
body, error = readfile(parsed.path)
@ -67,13 +58,13 @@ function getlinks(body, base)
body = string.gsub(body, "%<%!%-%-.-%-%-%>", "")
local links = {}
-- extract links
string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href)
body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href)
table.insert(links, socket.url.absolute(base, href))
end)
string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href)
body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href)
table.insert(links, socket.url.absolute(base, href))
end)
string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(%a+)", function(href)
string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href)
table.insert(links, socket.url.absolute(base, href))
end)
return links

View File

@ -71,12 +71,11 @@ function stats(size)
io.stderr:write("\r", gauge(got, delta, size))
io.stderr:flush()
end
return chunk
else
-- close up
io.stderr:write("\r", gauge(got, delta), "\n")
return ""
end
return chunk
end
end

View File

@ -44,6 +44,7 @@ int inet_open(lua_State *L)
luaL_openlib(L, NULL, func, 0);
lua_settable(L, -3);
lua_pop(L, 1);
return 0;
}
/*=========================================================================*\

View File

@ -127,7 +127,7 @@ function parse(url, default)
-- empty url is parsed to nil
if not url or url == "" then return nil end
-- remove whitespace
url = string.gsub(url, "%s", "")
-- url = string.gsub(url, "%s", "")
-- get fragment
url = string.gsub(url, "#(.*)$", function(f) parsed.fragment = f end)
-- get scheme
@ -139,6 +139,7 @@ function parse(url, default)
url = string.gsub(url, "%?(.*)", function(q) parsed.query = q end)
-- get params
url = string.gsub(url, "%;(.*)", function(p) parsed.params = p end)
-- path is whatever was left
if url ~= "" then parsed.path = url end
local authority = parsed.authority
if not authority then return parsed end
@ -164,7 +165,8 @@ end
-- a stringing with the corresponding URL
-----------------------------------------------------------------------------
function build(parsed)
local url = parsed.path or ""
local ppath = parse_path(parsed.path or "")
local url = build_path(ppath)
if parsed.params then url = url .. ";" .. parsed.params end
if parsed.query then url = url .. "?" .. parsed.query end
local authority = parsed.authority
@ -183,7 +185,7 @@ function build(parsed)
if authority then url = "//" .. authority .. url end
if parsed.scheme then url = parsed.scheme .. ":" .. url end
if parsed.fragment then url = url .. "#" .. parsed.fragment end
url = string.gsub(url, "%s", "")
-- url = string.gsub(url, "%s", "")
return url
end
@ -214,7 +216,7 @@ function absolute(base_url, relative_url)
end
end
else
relative.path = absolute_path(base.path,relative.path)
relative.path = absolute_path(base.path or "", relative.path)
end
end
return build(relative)
@ -231,7 +233,7 @@ end
function parse_path(path)
local parsed = {}
path = path or ""
path = string.gsub(path, "%s", "")
--path = string.gsub(path, "%s", "")
string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end)
for i = 1, table.getn(parsed) do
parsed[i] = unescape(parsed[i])

View File

@ -59,7 +59,7 @@ end
local check_absolute_url = function(base, relative, absolute)
local res = socket.url.absolute(base, relative)
if res ~= absolute then
write("absolute: In test for '", relative, "' expected '",
io.write("absolute: In test for '", relative, "' expected '",
absolute, "' but got '", res, "'\n")
exit()
end
@ -71,7 +71,7 @@ local check_parse_url = function(gaba)
local parsed = socket.url.parse(url)
for i, v in gaba do
if v ~= parsed[i] then
write("parse: In test for '", url, "' expected ", i, " = '",
io.write("parse: In test for '", url, "' expected ", i, " = '",
v, "' but got '", tostring(parsed[i]), "'\n")
for i,v in parsed do print(i,v) end
exit()
@ -79,7 +79,7 @@ local check_parse_url = function(gaba)
end
for i, v in parsed do
if v ~= gaba[i] then
write("parse: In test for '", url, "' expected ", i, " = '",
io.write("parse: In test for '", url, "' expected ", i, " = '",
tostring(gaba[i]), "' but got '", v, "'\n")
for i,v in parsed do print(i,v) end
exit()