Adjusted some of the broken examples.

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

View File

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

View File

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

View File

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

View File

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