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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,4 +1,5 @@
require"http" socket = require("socket")
http = require("http")
if not arg or not arg[1] or not arg[2] then if not arg or not arg[1] or not arg[2] then
print("luasocket cddb.lua <category> <disc-id> [<server>]") print("luasocket cddb.lua <category> <disc-id> [<server>]")
@ -31,7 +32,7 @@ end
local host = socket.dns.gethostname() local host = socket.dns.gethostname()
local query = "%s?cmd=cddb+read+%s+%s&hello=LuaSocket+%s+LuaSocket+2.0&proto=6" local query = "%s?cmd=cddb+read+%s+%s&hello=LuaSocket+%s+LuaSocket+2.0&proto=6"
local url = string.format(query, server, arg[1], arg[2], host) local url = string.format(query, server, arg[1], arg[2], host)
local body, headers, code, error = socket.http.get(url) local body, headers, code, error = http.get(url)
if code == 200 then if code == 200 then
local data, code, error = parse(body) local data, code, error = parse(body)

View File

@ -4,7 +4,7 @@
-- Author: Diego Nehab -- Author: Diego Nehab
-- RCS ID: $Id$ -- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
require"socket" local socket = require("socket")
host = host or "localhost" host = host or "localhost"
port = port or 7 port = port or 7
if arg then if arg then

View File

@ -4,7 +4,7 @@
-- Author: Diego Nehab -- Author: Diego Nehab
-- RCS ID: $Id$ -- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
require"socket" socket = require("socket")
host = host or "127.0.0.1" host = host or "127.0.0.1"
port = port or 7 port = port or 7
if arg then if arg then

View File

@ -4,7 +4,7 @@
-- Author: Diego Nehab -- Author: Diego Nehab
-- RCS ID: $Id$ -- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
require("socket") local socket = require("socket")
host = host or "*" host = host or "*"
port = port or 8080 port = port or 8080
if arg then if arg then

View File

@ -5,21 +5,22 @@
-- Conforming to: RFC 959, LTN7 -- Conforming to: RFC 959, LTN7
-- RCS ID: $Id$ -- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- make sure LuaSocket is loaded
require("socket")
-- get LuaSocket namespace
local socket = _G[LUASOCKET_LIBNAME]
-- require other modules -----------------------------------------------------------------------------
require("ltn12") -- Load other required modules
require("url") -----------------------------------------------------------------------------
require("tp") local socket = require("socket")
local ltn12 = require("ltn12")
local url = require("url")
local tp = require("tp")
-- create namespace inside LuaSocket namespace -----------------------------------------------------------------------------
socket.ftp = socket.ftp or {} -- Setup namespace
-----------------------------------------------------------------------------
local ftp = {}
-- make all module globals fall into namespace -- make all module globals fall into namespace
setmetatable(socket.ftp, { __index = _G }) setmetatable(ftp, { __index = _G })
setfenv(1, socket.ftp) setfenv(1, ftp)
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Program constants -- Program constants
@ -196,8 +197,8 @@ local default = {
scheme = "ftp" scheme = "ftp"
} }
local function parse(url) local function parse(u)
local putt = socket.try(socket.url.parse(url, default)) local putt = socket.try(url.parse(u, default))
socket.try(putt.scheme == "ftp", "invalid scheme '" .. putt.scheme .. "'") socket.try(putt.scheme == "ftp", "invalid scheme '" .. putt.scheme .. "'")
socket.try(putt.host, "invalid host") socket.try(putt.host, "invalid host")
local pat = "^type=(.)$" local pat = "^type=(.)$"
@ -208,8 +209,8 @@ local function parse(url)
return putt return putt
end end
local function sput(url, body) local function sput(u, body)
local putt = parse(url) local putt = parse(u)
putt.source = ltn12.source.string(body) putt.source = ltn12.source.string(body)
return tput(putt) return tput(putt)
end end
@ -230,8 +231,8 @@ local function tget(gett)
return ftp:close() return ftp:close()
end end
local function sget(url, body) local function sget(u, body)
local gett = parse(url) local gett = parse(u)
local t = {} local t = {}
gett.sink = ltn12.sink.table(t) gett.sink = ltn12.sink.table(t)
tget(gett) tget(gett)

View File

@ -5,23 +5,22 @@
-- Conforming to RFC 2616 -- Conforming to RFC 2616
-- RCS ID: $Id$ -- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- make sure LuaSocket is loaded
require("socket")
-- get LuaSocket namespace
local socket = _G[LUASOCKET_LIBNAME]
-- require other modules -----------------------------------------------------------------------------
require("ltn12") -- Load other required modules
require("mime") -------------------------------------------------------------------------------
-- get MIME namespace local socket = require("socket")
local mime = _G[MIME_LIBNAME] local ltn12 = require("ltn12")
require("url") local mime = require("mime")
local url = require("url")
-- create namespace inside LuaSocket namespace -----------------------------------------------------------------------------
socket.http = socket.http or {} -- Setup namespace
-------------------------------------------------------------------------------
http = {}
-- make all module globals fall into namespace -- make all module globals fall into namespace
setmetatable(socket.http, { __index = _G }) setmetatable(http, { __index = _G })
setfenv(1, socket.http) setfenv(1, http)
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Program constants -- Program constants
@ -116,17 +115,17 @@ local function receive_status(reqt, respt, tmp)
end end
local function request_uri(reqt, respt, tmp) local function request_uri(reqt, respt, tmp)
local url = tmp.parsed local u = tmp.parsed
if not reqt.proxy then if not reqt.proxy then
local parsed = tmp.parsed local parsed = tmp.parsed
url = { u = {
path = parsed.path, path = parsed.path,
params = parsed.params, params = parsed.params,
query = parsed.query, query = parsed.query,
fragment = parsed.fragment fragment = parsed.fragment
} }
end end
return socket.url.build(url) return url.build(u)
end end
local function send_request(reqt, respt, tmp) local function send_request(reqt, respt, tmp)
@ -155,7 +154,7 @@ local function open(reqt, respt, tmp)
local proxy = reqt.proxy or PROXY local proxy = reqt.proxy or PROXY
local host, port local host, port
if proxy then if proxy then
local pproxy = socket.url.parse(proxy) local pproxy = url.parse(proxy)
socket.try(pproxy.port and pproxy.host, "invalid proxy") socket.try(pproxy.port and pproxy.host, "invalid proxy")
host, port = pproxy.host, pproxy.port host, port = pproxy.host, pproxy.port
else else
@ -169,15 +168,13 @@ end
local function adjust_headers(reqt, respt, tmp) local function adjust_headers(reqt, respt, tmp)
local lower = {} local lower = {}
local headers = reqt.headers or {}
-- set default headers
lower["user-agent"] = USERAGENT
-- override with user values -- override with user values
for i,v in headers do for i,v in (reqt.headers or lower) do
lower[string.lower(i)] = v lower[string.lower(i)] = v
end end
lower["user-agent"] = lower["user-agent"] or USERAGENT
-- these cannot be overriden
lower["host"] = tmp.parsed.host lower["host"] = tmp.parsed.host
-- this cannot be overriden
lower["connection"] = "close" lower["connection"] = "close"
-- store results -- store results
tmp.headers = lower tmp.headers = lower
@ -185,7 +182,7 @@ end
local function parse_url(reqt, respt, tmp) local function parse_url(reqt, respt, tmp)
-- parse url with default fields -- parse url with default fields
local parsed = socket.url.parse(reqt.url, { local parsed = url.parse(reqt.url, {
host = "", host = "",
port = PORT, port = PORT,
path ="/", path ="/",
@ -250,7 +247,7 @@ local function redirect(reqt, respt, tmp)
method = reqt.method, method = reqt.method,
-- the RFC says the redirect URL has to be absolute, but some -- the RFC says the redirect URL has to be absolute, but some
-- servers do not respect that -- servers do not respect that
url = socket.url.absolute(reqt.url, respt.headers["location"]), url = url.absolute(reqt.url, respt.headers["location"]),
source = reqt.source, source = reqt.source,
sink = reqt.sink, sink = reqt.sink,
headers = reqt.headers, headers = reqt.headers,
@ -296,20 +293,20 @@ function request(reqt)
return respt return respt
end end
function get(url) function get(u)
local t = {} local t = {}
respt = request { respt = request {
url = url, url = u,
sink = ltn12.sink.table(t) sink = ltn12.sink.table(t)
} }
return (table.getn(t) > 0 or nil) and table.concat(t), respt.headers, return (table.getn(t) > 0 or nil) and table.concat(t), respt.headers,
respt.code, respt.error respt.code, respt.error
end end
function post(url, body) function post(u, body)
local t = {} local t = {}
respt = request { respt = request {
url = url, url = u,
method = "POST", method = "POST",
source = ltn12.source.string(body), source = ltn12.source.string(body),
sink = ltn12.sink.table(t), sink = ltn12.sink.table(t),
@ -318,3 +315,5 @@ function post(url, body)
return (table.getn(t) > 0 or nil) and table.concat(t), return (table.getn(t) > 0 or nil) and table.concat(t),
respt.headers, respt.code, respt.error respt.headers, respt.code, respt.error
end end
return http

View File

@ -37,13 +37,10 @@ static luaL_reg func[] = {
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
int inet_open(lua_State *L) int inet_open(lua_State *L)
{ {
lua_pushstring(L, LUASOCKET_LIBNAME);
lua_gettable(L, LUA_GLOBALSINDEX);
lua_pushstring(L, "dns"); lua_pushstring(L, "dns");
lua_newtable(L); lua_newtable(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);
return 0; return 0;
} }

View File

@ -1,10 +1,16 @@
-- create module namespace -----------------------------------------------------------------------------
ltn12 = ltn12 or {} -- LTN12 - Filters, sources, sinks and pumps.
-- make all globals fall into ltn12 namespace -- LuaSocket toolkit.
-- Author: Diego Nehab
-- RCS ID: $Id$
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-- Setup namespace
-----------------------------------------------------------------------------
local ltn12 = {}
setmetatable(ltn12, { __index = _G }) setmetatable(ltn12, { __index = _G })
setfenv(1, ltn12) setfenv(1, ltn12)
-- sub namespaces
filter = {} filter = {}
source = {} source = {}
sink = {} sink = {}
@ -13,15 +19,14 @@ pump = {}
-- 2048 seems to be better in windows... -- 2048 seems to be better in windows...
BLOCKSIZE = 2048 BLOCKSIZE = 2048
local function second(a, b)
return b
end
local function shift(a, b, c) local function shift(a, b, c)
return b, c return b, c
end end
-- returns a high level filter that cycles a cycles a low-level filter -----------------------------------------------------------------------------
-- Filter stuff
-----------------------------------------------------------------------------
-- returns a high level filter that cycles a low-level filter
function filter.cycle(low, ctx, extra) function filter.cycle(low, ctx, extra)
return function(chunk) return function(chunk)
local ret local ret
@ -61,6 +66,9 @@ function filter.chain(...)
return f return f
end end
-----------------------------------------------------------------------------
-- Source stuff
-----------------------------------------------------------------------------
-- create an empty source -- create an empty source
local function empty() local function empty()
return nil return nil
@ -162,6 +170,9 @@ function source.cat(...)
end end
end end
-----------------------------------------------------------------------------
-- Sink stuff
-----------------------------------------------------------------------------
-- creates a sink that stores into a table -- creates a sink that stores into a table
function sink.table(t) function sink.table(t)
t = t or {} t = t or {}
@ -224,6 +235,9 @@ function sink.chain(f, snk)
end end
end end
-----------------------------------------------------------------------------
-- Pump stuff
-----------------------------------------------------------------------------
-- pumps one chunk from the source to the sink -- pumps one chunk from the source to the sink
function pump.step(src, snk) function pump.step(src, snk)
local chunk, src_err = src() local chunk, src_err = src()
@ -239,3 +253,5 @@ function pump.all(src, snk, step)
if not ret then return not err, err end if not ret then return not err, err end
end end
end end
return ltn12

View File

@ -33,7 +33,6 @@
#include "tcp.h" #include "tcp.h"
#include "udp.h" #include "udp.h"
#include "select.h" #include "select.h"
#include "smtp.h"
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* Modules * Modules
@ -47,7 +46,6 @@ static const luaL_reg mod[] = {
{"tcp", tcp_open}, {"tcp", tcp_open},
{"udp", udp_open}, {"udp", udp_open},
{"select", select_open}, {"select", select_open},
{"smtp", smtp_open},
{NULL, NULL} {NULL, NULL}
}; };
@ -56,7 +54,6 @@ static const luaL_reg mod[] = {
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
LUASOCKET_API int luaopen_socket(lua_State *L) { LUASOCKET_API int luaopen_socket(lua_State *L) {
int i; int i;
for (i = 0; mod[i].name; i++) for (i = 0; mod[i].name; i++) mod[i].func(L);
mod[i].func(L);
return 1; return 1;
} }

View File

@ -15,13 +15,6 @@
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
#define LUASOCKET_VERSION "LuaSocket 2.0 (beta)" #define LUASOCKET_VERSION "LuaSocket 2.0 (beta)"
/*-------------------------------------------------------------------------*\
* Library's namespace
\*-------------------------------------------------------------------------*/
#ifndef LUASOCKET_LIBNAME
#define LUASOCKET_LIBNAME "socket"
#endif
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\
* This macro prefixes all exported API functions * This macro prefixes all exported API functions
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/

View File

@ -76,9 +76,8 @@ static UC b64unbase[256];
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
MIME_API int luaopen_mime(lua_State *L) MIME_API int luaopen_mime(lua_State *L)
{ {
lua_pushstring(L, MIME_LIBNAME); lua_newtable(L);
lua_setglobal(L, "MIME_LIBNAME"); luaL_openlib(L, NULL, func, 0);
luaL_openlib(L, MIME_LIBNAME, func, 0);
/* initialize lookup tables */ /* initialize lookup tables */
qpsetup(qpclass, qpunbase); qpsetup(qpclass, qpunbase);
b64setup(b64unbase); b64setup(b64unbase);
@ -626,7 +625,6 @@ static int eolprocess(int c, int last, const char *marker,
luaL_putchar(buffer, c); luaL_putchar(buffer, c);
return 0; return 0;
} }
} }
/*-------------------------------------------------------------------------*\ /*-------------------------------------------------------------------------*\

View File

@ -21,11 +21,4 @@
MIME_API int luaopen_mime(lua_State *L); MIME_API int luaopen_mime(lua_State *L);
/*-------------------------------------------------------------------------*\
* Library's namespace
\*-------------------------------------------------------------------------*/
#ifndef MIME_LIBNAME
#define MIME_LIBNAME "mime"
#endif
#endif /* MIME_H */ #endif /* MIME_H */

View File

@ -9,19 +9,17 @@
-- Load MIME from dynamic library -- Load MIME from dynamic library
-- Comment these lines if you are loading static -- Comment these lines if you are loading static
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
open, err1, err2 = loadlib("mime", "luaopen_mime") local open = assert(loadlib("mime", "luaopen_mime"))
if not open then error(err1) end local mime = assert(open())
open()
if not MIME_LIBNAME then error("MIME init failed") end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Namespace independence -- Load other required modules
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
local mime = _G[MIME_LIBNAME] local ltn12 = require("ltn12")
if not mime then error('MIME init FAILED') end
require("ltn12")
-----------------------------------------------------------------------------
-- Setup namespace
-----------------------------------------------------------------------------
-- make all module globals fall into mime namespace -- make all module globals fall into mime namespace
setmetatable(mime, { __index = _G }) setmetatable(mime, { __index = _G })
setfenv(1, mime) setfenv(1, mime)

View File

@ -50,8 +50,7 @@ int select_open(lua_State *L)
#else #else
lua_dofile(L, "select.lua"); lua_dofile(L, "select.lua");
#endif #endif
luaL_openlib(L, LUASOCKET_LIBNAME, func, 1); luaL_openlib(L, NULL, func, 1);
lua_pop(L, 1);
aux_newclass(L, "select{fd_set}", set); aux_newclass(L, "select{fd_set}", set);
return 0; return 0;
} }

View File

@ -5,22 +5,28 @@
-- Conforming to RFC 2821 -- Conforming to RFC 2821
-- RCS ID: $Id$ -- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- make sure LuaSocket is loaded
require("socket")
-- get LuaSocket namespace
local socket = _G[LUASOCKET_LIBNAME]
require("ltn12") -----------------------------------------------------------------------------
require("tp") -- Load SMTP from dynamic library
-- Comment these lines if you are loading static
-----------------------------------------------------------------------------
local open = assert(loadlib("smtp", "luaopen_smtp"))
local smtp = assert(open())
-- create smtp namespace inside LuaSocket namespace -----------------------------------------------------------------------------
local smtp = socket.smtp or {} -- Load other required modules
socket.smtp = smtp -----------------------------------------------------------------------------
local socket = require("socket")
local ltn12 = require("ltn12")
local tp = require("tp")
-----------------------------------------------------------------------------
-- Setup namespace
-----------------------------------------------------------------------------
-- make all module globals fall into smtp namespace -- make all module globals fall into smtp namespace
setmetatable(smtp, { __index = _G }) setmetatable(smtp, { __index = _G })
setfenv(1, smtp) setfenv(1, smtp)
-- default server used to send e-mails -- default server used to send e-mails
SERVER = "localhost" SERVER = "localhost"
-- default port -- default port
@ -89,7 +95,7 @@ end
function open(server, port) function open(server, port)
print(server or SERVER, port or PORT) print(server or SERVER, port or PORT)
local tp, error = socket.tp.connect(server or SERVER, port or PORT) local tp, error = tp.connect(server or SERVER, port or PORT)
if not tp then return nil, error end if not tp then return nil, error end
return setmetatable({tp = tp}, metat) return setmetatable({tp = tp}, metat)
end end
@ -176,11 +182,16 @@ end
-- set defaul headers -- set defaul headers
local function adjust_headers(mesgt) local function adjust_headers(mesgt)
mesgt.headers = mesgt.headers or {} local lower = {}
mesgt.headers["mime-version"] = "1.0" for i,v in (mesgt or lower) do
mesgt.headers["date"] = mesgt.headers["date"] or lower[string.lower(i)] = v
end
lower["date"] = lower["date"] or
os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or ZONE) os.date("!%a, %d %b %Y %H:%M:%S ") .. (mesgt.zone or ZONE)
mesgt.headers["x-mailer"] = mesgt.headers["x-mailer"] or socket.version lower["x-mailer"] = lower["x-mailer"] or socket.version
-- this can't be overriden
lower["mime-version"] = "1.0"
mesgt.headers = lower
end end
function message(mesgt) function message(mesgt)

View File

@ -6,18 +6,9 @@
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Load LuaSocket from dynamic library -- Load LuaSocket from dynamic library
-- Comment these lines if you are loading static
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
open, err1, err2 = loadlib("luasocket", "luaopen_socket") local open = assert(loadlib("luasocket", "luaopen_socket"))
if not open then error(err1) end local socket = assert(open())
open()
if not LUASOCKET_LIBNAME then error("LuaSocket init failed") end
-----------------------------------------------------------------------------
-- Namespace independence
-----------------------------------------------------------------------------
local socket = _G[LUASOCKET_LIBNAME]
if not socket then error('LuaSocket init failed') end
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- Auxiliar functions -- Auxiliar functions
@ -172,3 +163,5 @@ end
socket.sourcet["default"] = socket.sourcet["until-closed"] socket.sourcet["default"] = socket.sourcet["until-closed"]
socket.source = socket.choose(socket.sourcet) socket.source = socket.choose(socket.sourcet)
return socket

View File

@ -96,8 +96,7 @@ int tcp_open(lua_State *L)
aux_add2group(L, "tcp{client}", "select{able}"); aux_add2group(L, "tcp{client}", "select{able}");
aux_add2group(L, "tcp{server}", "select{able}"); aux_add2group(L, "tcp{server}", "select{able}");
/* define library functions */ /* define library functions */
luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); luaL_openlib(L, NULL, func, 0);
lua_pop(L, 1);
return 0; return 0;
} }

View File

@ -143,8 +143,7 @@ int tm_gettime(void)
\*-------------------------------------------------------------------------*/ \*-------------------------------------------------------------------------*/
int tm_open(lua_State *L) int tm_open(lua_State *L)
{ {
luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); luaL_openlib(L, NULL, func, 0);
lua_pop(L, 1);
return 0; return 0;
} }

View File

@ -5,16 +5,18 @@
-- Conforming to: RFC 2616, LTN7 -- Conforming to: RFC 2616, LTN7
-- RCS ID: $Id$ -- RCS ID: $Id$
----------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- make sure LuaSocket is loaded
require("socket")
-- get LuaSocket namespace
local socket = _G[LUASOCKET_LIBNAME]
-- create namespace inside LuaSocket namespace -----------------------------------------------------------------------------
socket.tp = socket.tp or {} -- Load other required modules
-- make all module globals fall into namespace -----------------------------------------------------------------------------
setmetatable(socket.tp, { __index = _G }) local socket = require("socket")
setfenv(1, socket.tp)
-----------------------------------------------------------------------------
-- Setup namespace
-----------------------------------------------------------------------------
tp = {}
setmetatable(tp, { __index = _G })
setfenv(1, tp)
TIMEOUT = 60 TIMEOUT = 60
@ -107,3 +109,5 @@ function connect(host, port)
control:settimeout(TIMEOUT) control:settimeout(TIMEOUT)
return setmetatable({control = control}, metat) return setmetatable({control = control}, metat)
end end
return tp

View File

@ -90,8 +90,7 @@ int udp_open(lua_State *L)
aux_add2group(L, "udp{connected}", "select{able}"); aux_add2group(L, "udp{connected}", "select{able}");
aux_add2group(L, "udp{unconnected}", "select{able}"); aux_add2group(L, "udp{unconnected}", "select{able}");
/* define library functions */ /* define library functions */
luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); luaL_openlib(L, NULL, func, 0);
lua_pop(L, 1);
return 0; return 0;
} }

View File

@ -4,16 +4,12 @@
-- Author: Diego Nehab -- Author: Diego Nehab
-- Conforming to: RFC 2396, LTN7 -- Conforming to: RFC 2396, LTN7
-- RCS ID: $Id$ -- RCS ID: $Id$
---------------------------------------------------------------------------- -----------------------------------------------------------------------------
-- make sure LuaSocket is loaded
require("socket")
-- get LuaSocket namespace
local socket = _G[LUASOCKET_LIBNAME]
-- create url namespace inside LuaSocket namespace -----------------------------------------------------------------------------
local url = socket.url or {} -- Setup namespace
socket.url = url -----------------------------------------------------------------------------
-- make all module globals fall into url namespace local url = {}
setmetatable(url, { __index = _G }) setmetatable(url, { __index = _G })
setfenv(1, url) setfenv(1, url)
@ -275,3 +271,5 @@ function build_path(parsed, unsafe)
if parsed.is_absolute then path = "/" .. path end if parsed.is_absolute then path = "/" .. path end
return path return path
end end
return url

View File

@ -1,6 +1,6 @@
require("socket") local socket = require("socket")
require("ltn12") local ltn12 = require("ltn12")
require("mime") local mime = require("mime")
dofile("testsupport.lua") dofile("testsupport.lua")