From 9ed7f955e5fc69af9bf1794fa2c8cd227981ba24 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Fri, 4 Jun 2004 15:15:45 +0000 Subject: Só pra não perder se der merda. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ftp.lua | 41 +++++++++++++++++++++-------------------- src/http.lua | 57 ++++++++++++++++++++++++++++----------------------------- src/inet.c | 3 --- src/ltn12.lua | 36 ++++++++++++++++++++++++++---------- src/luasocket.c | 5 +---- src/luasocket.h | 7 ------- src/mime.c | 6 ++---- src/mime.h | 7 ------- src/mime.lua | 16 +++++++--------- src/select.c | 3 +-- src/smtp.lua | 41 ++++++++++++++++++++++++++--------------- src/socket.lua | 15 ++++----------- src/tcp.c | 3 +-- src/timeout.c | 3 +-- src/tp.lua | 22 +++++++++++++--------- src/udp.c | 3 +-- src/url.lua | 16 +++++++--------- 17 files changed, 139 insertions(+), 145 deletions(-) (limited to 'src') diff --git a/src/ftp.lua b/src/ftp.lua index 842fdbb..79772f8 100644 --- a/src/ftp.lua +++ b/src/ftp.lua @@ -5,21 +5,22 @@ -- Conforming to: RFC 959, LTN7 -- RCS ID: $Id$ ----------------------------------------------------------------------------- --- make sure LuaSocket is loaded -require("socket") --- get LuaSocket namespace -local socket = _G[LUASOCKET_LIBNAME] - --- require other modules -require("ltn12") -require("url") -require("tp") - --- create namespace inside LuaSocket namespace -socket.ftp = socket.ftp or {} + +----------------------------------------------------------------------------- +-- Load other required modules +----------------------------------------------------------------------------- +local socket = require("socket") +local ltn12 = require("ltn12") +local url = require("url") +local tp = require("tp") + +----------------------------------------------------------------------------- +-- Setup namespace +----------------------------------------------------------------------------- +local ftp = {} -- make all module globals fall into namespace -setmetatable(socket.ftp, { __index = _G }) -setfenv(1, socket.ftp) +setmetatable(ftp, { __index = _G }) +setfenv(1, ftp) ----------------------------------------------------------------------------- -- Program constants @@ -196,8 +197,8 @@ local default = { scheme = "ftp" } -local function parse(url) - local putt = socket.try(socket.url.parse(url, default)) +local function parse(u) + local putt = socket.try(url.parse(u, default)) socket.try(putt.scheme == "ftp", "invalid scheme '" .. putt.scheme .. "'") socket.try(putt.host, "invalid host") local pat = "^type=(.)$" @@ -208,8 +209,8 @@ local function parse(url) return putt end -local function sput(url, body) - local putt = parse(url) +local function sput(u, body) + local putt = parse(u) putt.source = ltn12.source.string(body) return tput(putt) end @@ -230,8 +231,8 @@ local function tget(gett) return ftp:close() end -local function sget(url, body) - local gett = parse(url) +local function sget(u, body) + local gett = parse(u) local t = {} gett.sink = ltn12.sink.table(t) tget(gett) diff --git a/src/http.lua b/src/http.lua index 66a236d..ebe6b54 100644 --- a/src/http.lua +++ b/src/http.lua @@ -5,23 +5,22 @@ -- Conforming to RFC 2616 -- RCS ID: $Id$ ----------------------------------------------------------------------------- --- make sure LuaSocket is loaded -require("socket") --- get LuaSocket namespace -local socket = _G[LUASOCKET_LIBNAME] --- require other modules -require("ltn12") -require("mime") --- get MIME namespace -local mime = _G[MIME_LIBNAME] -require("url") +----------------------------------------------------------------------------- +-- Load other required modules +------------------------------------------------------------------------------- +local socket = require("socket") +local ltn12 = require("ltn12") +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 -setmetatable(socket.http, { __index = _G }) -setfenv(1, socket.http) +setmetatable(http, { __index = _G }) +setfenv(1, http) ----------------------------------------------------------------------------- -- Program constants @@ -116,17 +115,17 @@ local function receive_status(reqt, respt, tmp) end local function request_uri(reqt, respt, tmp) - local url = tmp.parsed + local u = tmp.parsed if not reqt.proxy then local parsed = tmp.parsed - url = { + u = { path = parsed.path, params = parsed.params, query = parsed.query, fragment = parsed.fragment } end - return socket.url.build(url) + return url.build(u) end local function send_request(reqt, respt, tmp) @@ -155,7 +154,7 @@ local function open(reqt, respt, tmp) local proxy = reqt.proxy or PROXY local host, port if proxy then - local pproxy = socket.url.parse(proxy) + local pproxy = url.parse(proxy) socket.try(pproxy.port and pproxy.host, "invalid proxy") host, port = pproxy.host, pproxy.port else @@ -169,15 +168,13 @@ end local function adjust_headers(reqt, respt, tmp) local lower = {} - local headers = reqt.headers or {} - -- set default headers - lower["user-agent"] = USERAGENT -- override with user values - for i,v in headers do + for i,v in (reqt.headers or lower) do lower[string.lower(i)] = v end + lower["user-agent"] = lower["user-agent"] or USERAGENT + -- these cannot be overriden lower["host"] = tmp.parsed.host - -- this cannot be overriden lower["connection"] = "close" -- store results tmp.headers = lower @@ -185,7 +182,7 @@ end local function parse_url(reqt, respt, tmp) -- parse url with default fields - local parsed = socket.url.parse(reqt.url, { + local parsed = url.parse(reqt.url, { host = "", port = PORT, path ="/", @@ -250,7 +247,7 @@ local function redirect(reqt, respt, tmp) method = reqt.method, -- the RFC says the redirect URL has to be absolute, but some -- 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, sink = reqt.sink, headers = reqt.headers, @@ -296,20 +293,20 @@ function request(reqt) return respt end -function get(url) +function get(u) local t = {} respt = request { - url = url, + url = u, sink = ltn12.sink.table(t) } return (table.getn(t) > 0 or nil) and table.concat(t), respt.headers, respt.code, respt.error end -function post(url, body) +function post(u, body) local t = {} respt = request { - url = url, + url = u, method = "POST", source = ltn12.source.string(body), sink = ltn12.sink.table(t), @@ -318,3 +315,5 @@ function post(url, body) return (table.getn(t) > 0 or nil) and table.concat(t), respt.headers, respt.code, respt.error end + +return http diff --git a/src/inet.c b/src/inet.c index 8941575..3a57441 100644 --- a/src/inet.c +++ b/src/inet.c @@ -37,13 +37,10 @@ static luaL_reg func[] = { \*-------------------------------------------------------------------------*/ int inet_open(lua_State *L) { - lua_pushstring(L, LUASOCKET_LIBNAME); - lua_gettable(L, LUA_GLOBALSINDEX); lua_pushstring(L, "dns"); lua_newtable(L); luaL_openlib(L, NULL, func, 0); lua_settable(L, -3); - lua_pop(L, 1); return 0; } diff --git a/src/ltn12.lua b/src/ltn12.lua index 56e6043..41855f0 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua @@ -1,10 +1,16 @@ --- create module namespace -ltn12 = ltn12 or {} --- make all globals fall into ltn12 namespace +----------------------------------------------------------------------------- +-- LTN12 - Filters, sources, sinks and pumps. +-- LuaSocket toolkit. +-- Author: Diego Nehab +-- RCS ID: $Id$ +----------------------------------------------------------------------------- + +----------------------------------------------------------------------------- +-- Setup namespace +----------------------------------------------------------------------------- +local ltn12 = {} setmetatable(ltn12, { __index = _G }) setfenv(1, ltn12) - --- sub namespaces filter = {} source = {} sink = {} @@ -13,15 +19,14 @@ pump = {} -- 2048 seems to be better in windows... BLOCKSIZE = 2048 -local function second(a, b) - return b -end - local function shift(a, b, c) return b, c 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) return function(chunk) local ret @@ -61,6 +66,9 @@ function filter.chain(...) return f end +----------------------------------------------------------------------------- +-- Source stuff +----------------------------------------------------------------------------- -- create an empty source local function empty() return nil @@ -162,6 +170,9 @@ function source.cat(...) end end +----------------------------------------------------------------------------- +-- Sink stuff +----------------------------------------------------------------------------- -- creates a sink that stores into a table function sink.table(t) t = t or {} @@ -224,6 +235,9 @@ function sink.chain(f, snk) end end +----------------------------------------------------------------------------- +-- Pump stuff +----------------------------------------------------------------------------- -- pumps one chunk from the source to the sink function pump.step(src, snk) local chunk, src_err = src() @@ -239,3 +253,5 @@ function pump.all(src, snk, step) if not ret then return not err, err end end end + +return ltn12 diff --git a/src/luasocket.c b/src/luasocket.c index a5b6cb0..ca3a52c 100644 --- a/src/luasocket.c +++ b/src/luasocket.c @@ -33,7 +33,6 @@ #include "tcp.h" #include "udp.h" #include "select.h" -#include "smtp.h" /*-------------------------------------------------------------------------*\ * Modules @@ -47,7 +46,6 @@ static const luaL_reg mod[] = { {"tcp", tcp_open}, {"udp", udp_open}, {"select", select_open}, - {"smtp", smtp_open}, {NULL, NULL} }; @@ -56,7 +54,6 @@ static const luaL_reg mod[] = { \*-------------------------------------------------------------------------*/ LUASOCKET_API int luaopen_socket(lua_State *L) { int i; - for (i = 0; mod[i].name; i++) - mod[i].func(L); + for (i = 0; mod[i].name; i++) mod[i].func(L); return 1; } diff --git a/src/luasocket.h b/src/luasocket.h index 81d7b3f..716b7ff 100644 --- a/src/luasocket.h +++ b/src/luasocket.h @@ -15,13 +15,6 @@ \*-------------------------------------------------------------------------*/ #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 \*-------------------------------------------------------------------------*/ diff --git a/src/mime.c b/src/mime.c index 966509b..f42528c 100644 --- a/src/mime.c +++ b/src/mime.c @@ -76,9 +76,8 @@ static UC b64unbase[256]; \*-------------------------------------------------------------------------*/ MIME_API int luaopen_mime(lua_State *L) { - lua_pushstring(L, MIME_LIBNAME); - lua_setglobal(L, "MIME_LIBNAME"); - luaL_openlib(L, MIME_LIBNAME, func, 0); + lua_newtable(L); + luaL_openlib(L, NULL, func, 0); /* initialize lookup tables */ qpsetup(qpclass, qpunbase); b64setup(b64unbase); @@ -626,7 +625,6 @@ static int eolprocess(int c, int last, const char *marker, luaL_putchar(buffer, c); return 0; } - } /*-------------------------------------------------------------------------*\ diff --git a/src/mime.h b/src/mime.h index 35389f0..6febedf 100644 --- a/src/mime.h +++ b/src/mime.h @@ -21,11 +21,4 @@ MIME_API int luaopen_mime(lua_State *L); -/*-------------------------------------------------------------------------*\ -* Library's namespace -\*-------------------------------------------------------------------------*/ -#ifndef MIME_LIBNAME -#define MIME_LIBNAME "mime" -#endif - #endif /* MIME_H */ diff --git a/src/mime.lua b/src/mime.lua index 8f2cfff..ecf310d 100644 --- a/src/mime.lua +++ b/src/mime.lua @@ -9,19 +9,17 @@ -- Load MIME from dynamic library -- Comment these lines if you are loading static ----------------------------------------------------------------------------- -open, err1, err2 = loadlib("mime", "luaopen_mime") -if not open then error(err1) end -open() -if not MIME_LIBNAME then error("MIME init failed") end +local open = assert(loadlib("mime", "luaopen_mime")) +local mime = assert(open()) ----------------------------------------------------------------------------- --- Namespace independence +-- Load other required modules ----------------------------------------------------------------------------- -local mime = _G[MIME_LIBNAME] -if not mime then error('MIME init FAILED') end - -require("ltn12") +local ltn12 = require("ltn12") +----------------------------------------------------------------------------- +-- Setup namespace +----------------------------------------------------------------------------- -- make all module globals fall into mime namespace setmetatable(mime, { __index = _G }) setfenv(1, mime) diff --git a/src/select.c b/src/select.c index 41bdaa4..1ebd82c 100644 --- a/src/select.c +++ b/src/select.c @@ -50,8 +50,7 @@ int select_open(lua_State *L) #else lua_dofile(L, "select.lua"); #endif - luaL_openlib(L, LUASOCKET_LIBNAME, func, 1); - lua_pop(L, 1); + luaL_openlib(L, NULL, func, 1); aux_newclass(L, "select{fd_set}", set); return 0; } diff --git a/src/smtp.lua b/src/smtp.lua index 3108395..7ae99a5 100644 --- a/src/smtp.lua +++ b/src/smtp.lua @@ -5,22 +5,28 @@ -- Conforming to RFC 2821 -- 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()) + +----------------------------------------------------------------------------- +-- Load other required modules +----------------------------------------------------------------------------- +local socket = require("socket") +local ltn12 = require("ltn12") +local tp = require("tp") --- create smtp namespace inside LuaSocket namespace -local smtp = socket.smtp or {} -socket.smtp = smtp +----------------------------------------------------------------------------- +-- Setup namespace +----------------------------------------------------------------------------- -- make all module globals fall into smtp namespace setmetatable(smtp, { __index = _G }) setfenv(1, smtp) - -- default server used to send e-mails SERVER = "localhost" -- default port @@ -89,7 +95,7 @@ end function open(server, 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 return setmetatable({tp = tp}, metat) end @@ -176,11 +182,16 @@ end -- set defaul headers local function adjust_headers(mesgt) - mesgt.headers = mesgt.headers or {} - mesgt.headers["mime-version"] = "1.0" - mesgt.headers["date"] = mesgt.headers["date"] or + local lower = {} + for i,v in (mesgt or lower) do + lower[string.lower(i)] = v + end + lower["date"] = lower["date"] or 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 function message(mesgt) diff --git a/src/socket.lua b/src/socket.lua index e6e20f2..418cd1b 100644 --- a/src/socket.lua +++ b/src/socket.lua @@ -6,18 +6,9 @@ ----------------------------------------------------------------------------- -- Load LuaSocket from dynamic library --- Comment these lines if you are loading static ----------------------------------------------------------------------------- -open, err1, err2 = loadlib("luasocket", "luaopen_socket") -if not open then error(err1) end -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 +local open = assert(loadlib("luasocket", "luaopen_socket")) +local socket = assert(open()) ----------------------------------------------------------------------------- -- Auxiliar functions @@ -172,3 +163,5 @@ end socket.sourcet["default"] = socket.sourcet["until-closed"] socket.source = socket.choose(socket.sourcet) + +return socket diff --git a/src/tcp.c b/src/tcp.c index d0bc957..90cfcde 100644 --- a/src/tcp.c +++ b/src/tcp.c @@ -96,8 +96,7 @@ int tcp_open(lua_State *L) aux_add2group(L, "tcp{client}", "select{able}"); aux_add2group(L, "tcp{server}", "select{able}"); /* define library functions */ - luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); - lua_pop(L, 1); + luaL_openlib(L, NULL, func, 0); return 0; } diff --git a/src/timeout.c b/src/timeout.c index 1d710dc..bd6c3b4 100644 --- a/src/timeout.c +++ b/src/timeout.c @@ -143,8 +143,7 @@ int tm_gettime(void) \*-------------------------------------------------------------------------*/ int tm_open(lua_State *L) { - luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); - lua_pop(L, 1); + luaL_openlib(L, NULL, func, 0); return 0; } diff --git a/src/tp.lua b/src/tp.lua index f510226..3e9dba6 100644 --- a/src/tp.lua +++ b/src/tp.lua @@ -5,16 +5,18 @@ -- Conforming to: RFC 2616, LTN7 -- 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 {} --- make all module globals fall into namespace -setmetatable(socket.tp, { __index = _G }) -setfenv(1, socket.tp) +----------------------------------------------------------------------------- +-- Load other required modules +----------------------------------------------------------------------------- +local socket = require("socket") + +----------------------------------------------------------------------------- +-- Setup namespace +----------------------------------------------------------------------------- +tp = {} +setmetatable(tp, { __index = _G }) +setfenv(1, tp) TIMEOUT = 60 @@ -107,3 +109,5 @@ function connect(host, port) control:settimeout(TIMEOUT) return setmetatable({control = control}, metat) end + +return tp diff --git a/src/udp.c b/src/udp.c index 19cefe6..4770a2e 100644 --- a/src/udp.c +++ b/src/udp.c @@ -90,8 +90,7 @@ int udp_open(lua_State *L) aux_add2group(L, "udp{connected}", "select{able}"); aux_add2group(L, "udp{unconnected}", "select{able}"); /* define library functions */ - luaL_openlib(L, LUASOCKET_LIBNAME, func, 0); - lua_pop(L, 1); + luaL_openlib(L, NULL, func, 0); return 0; } diff --git a/src/url.lua b/src/url.lua index 8c591c0..2441268 100644 --- a/src/url.lua +++ b/src/url.lua @@ -4,16 +4,12 @@ -- Author: Diego Nehab -- Conforming to: RFC 2396, LTN7 -- 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 {} -socket.url = url --- make all module globals fall into url namespace +----------------------------------------------------------------------------- +-- Setup namespace +----------------------------------------------------------------------------- +local url = {} setmetatable(url, { __index = _G }) setfenv(1, url) @@ -275,3 +271,5 @@ function build_path(parsed, unsafe) if parsed.is_absolute then path = "/" .. path end return path end + +return url -- cgit v1.2.3-55-g6feb