Só pra não perder se der merda.

This commit is contained in:
Diego Nehab 2004-06-04 15:15:45 +00:00
parent 63d60223da
commit 9ed7f955e5
28 changed files with 194 additions and 200 deletions

View file

@ -1,7 +1,7 @@
This directory contains code that is more useful than the examples. This code
*is* supported.
lua.lua and luasocket.lua
lua.lua
These are modules to suport dynamic loading of LuaSocket by the stand alone
Lua Interpreter with the use of the "require" function. For my Mac OS X

View file

@ -1,5 +1,5 @@
require("ltn12")
require("mime")
local ltn12 = require("ltn12")
local mime = require("mime")
local source = ltn12.source.file(io.stdin)
local sink = ltn12.sink.file(io.stdout)
local convert

View file

@ -4,15 +4,14 @@
-- Author: Diego Nehab
-- RCS ID: $Id$
-----------------------------------------------------------------------------
require"http"
socket.http.TIMEOUT = 10
local http = require("http")
local url = require("url")
http.TIMEOUT = 10
cache = {}
function readfile(path)
path = socket.url.unescape(path)
path = url.unescape(path)
local file, error = io.open(path, "r")
if file then
local body = file:read("*a")
@ -21,32 +20,32 @@ function readfile(path)
else return nil, error end
end
function getstatus(url)
local parsed = socket.url.parse(url, { scheme = "file" })
if cache[url] then return cache[url] end
function getstatus(u)
local parsed = url.parse(u, {scheme = "file"})
if cache[u] then return cache[u] end
local res
if parsed.scheme == "http" then
local request = { url = url, method = "HEAD" }
local response = socket.http.request(request)
local request = {url = u, method = "HEAD"}
local response = 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 = io.open(socket.url.unescape(parsed.path), "r")
local file, error = io.open(url.unescape(parsed.path), "r")
if file then
file:close()
res = nil
else res = error end
else res = string.format("unhandled scheme '%s'", parsed.scheme) end
cache[url] = res
cache[u] = res
return res
end
function retrieve(url)
local parsed = socket.url.parse(url, { scheme = "file" })
function retrieve(u)
local parsed = url.parse(u, { scheme = "file" })
local body, headers, code, error
local base = url
local base = u
if parsed.scheme == "http" then
body, headers, code, error = socket.http.get(url)
body, headers, code, error = http.get(u)
if code == 200 then
base = base or headers.location
end
@ -62,19 +61,19 @@ function getlinks(body, base)
local links = {}
-- extract links
body = string.gsub(body, '[Hh][Rr][Ee][Ff]%s*=%s*"([^"]*)"', function(href)
table.insert(links, socket.url.absolute(base, href))
table.insert(links, url.absolute(base, href))
end)
body = string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*'([^']*)'", function(href)
table.insert(links, socket.url.absolute(base, href))
table.insert(links, url.absolute(base, href))
end)
string.gsub(body, "[Hh][Rr][Ee][Ff]%s*=%s*(.-)>", function(href)
table.insert(links, socket.url.absolute(base, href))
table.insert(links, url.absolute(base, href))
end)
return links
end
function checklinks(url)
local base, body, error = retrieve(url)
function checklinks(u)
local base, body, error = retrieve(u)
if not body then print(error) return end
local links = getlinks(body, base)
for _, l in ipairs(links) do
@ -91,5 +90,5 @@ if table.getn(arg) < 1 then
end
for _, a in ipairs(arg) do
print("Checking ", a)
checklinks(socket.url.absolute("file:", a))
checklinks(url.absolute("file:", a))
end

View file

@ -4,7 +4,7 @@
-- Author: Diego Nehab
-- RCS ID: $Id$
-----------------------------------------------------------------------------
require"socket"
local socket = require("socket")
function get_status(sock, valid)
local line, err = sock:receive()
@ -77,5 +77,5 @@ if arg and arg[1] then
defs, err = dict_get(arg[1], arg[2])
print(defs or err)
else
io.write("Usage:\n luasocket dict.lua <word> [<dictionary>]\n")
io.write("Usage:\n lua dict.lua <word> [<dictionary>]\n")
end

View file

@ -1,6 +1,5 @@
require"mime.lua"
require"ltn12.lua"
local mime = require("mime")
local ltn12 = require("ltn12")
local marker = '\n'
if arg and arg[1] == '-d' then marker = '\r\n' end
local filter = mime.normalize(marker)

View file

@ -4,9 +4,10 @@
-- Author: Diego Nehab
-- RCS ID: $Id$
-----------------------------------------------------------------------------
require"http"
require"ftp"
require"url"
socket = require("socket")
http = require("http")
ftp = require("ftp")
url = require("url")
-- formats a number of seconds into human readable form
function nicetime(s)
@ -84,55 +85,55 @@ function stats(size)
end
-- determines the size of a http file
function gethttpsize(url)
local respt = socket.http.request {method = "HEAD", url = url}
function gethttpsize(u)
local respt = http.request {method = "HEAD", url = u}
if respt.code == 200 then
return tonumber(respt.headers["content-length"])
end
end
-- downloads a file using the http protocol
function getbyhttp(url, file)
function getbyhttp(u, file)
local save = ltn12.sink.file(file or io.stdout)
-- only print feedback if output is not stdout
if file then save = ltn12.sink.chain(stats(gethttpsize(url)), save) end
local respt = socket.http.request {url = url, sink = save }
if file then save = ltn12.sink.chain(stats(gethttpsize(u)), save) end
local respt = http.request {url = u, sink = save }
if respt.code ~= 200 then print(respt.status or respt.error) end
end
-- downloads a file using the ftp protocol
function getbyftp(url, file)
function getbyftp(u, file)
local save = ltn12.sink.file(file or io.stdout)
-- only print feedback if output is not stdout
-- and we don't know how big the file is
if file then save = ltn12.sink.chain(stats(), save) end
local gett = socket.url.parse(url)
local gett = url.parse(u)
gett.sink = save
gett.type = "i"
local ret, err = socket.ftp.get(gett)
local ret, err = ftp.get(gett)
if err then print(err) end
end
-- determines the scheme
function getscheme(url)
function getscheme(u)
-- this is an heuristic to solve a common invalid url poblem
if not string.find(url, "//") then url = "//" .. url end
local parsed = socket.url.parse(url, {scheme = "http"})
if not string.find(u, "//") then u = "//" .. u end
local parsed = url.parse(u, {scheme = "http"})
return parsed.scheme
end
-- gets a file either by http or ftp, saving as <name>
function get(url, name)
function get(u, name)
local fout = name and io.open(name, "wb")
local scheme = getscheme(url)
if scheme == "ftp" then getbyftp(url, fout)
elseif scheme == "http" then getbyhttp(url, fout)
local scheme = getscheme(u)
if scheme == "ftp" then getbyftp(u, fout)
elseif scheme == "http" then getbyhttp(u, fout)
else print("unknown scheme" .. scheme) end
end
-- main program
arg = arg or {}
if table.getn(arg) < 1 then
io.write("Usage:\n luasocket get.lua <remote-url> [<local-file>]\n")
io.write("Usage:\n lua get.lua <remote-url> [<local-file>]\n")
os.exit(1)
else get(arg[1], arg[2]) end