diff --git a/TODO b/TODO index 33e9a43..817d471 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,8 @@ + ajeitar os README.* ajeitar as referencias a RFCS e LTNS em todos os arquivos. +smtp.o goes to mime.dll make sure sockets are closed when exceptions are raised check garbage collection in test*.lua diff --git a/doc/mime.html b/doc/mime.html index e7211fa..fe15df3 100644 --- a/doc/mime.html +++ b/doc/mime.html @@ -230,6 +230,35 @@ print((mime.b64("diego:password"))) --> ZGllZ286cGFzc3dvcmQ= + +

+A, B = mime.dot(C [, D]) +

+ +

+Low-level filter to perform Base64 encoding. +

+ +

+A is the encoded version of the largest prefix of +C..D +that can be encoded unambiguously. B has the remaining bytes of +C..D, before encoding. +If D is nil, A is padded with +the encoding of the remaining bytes of C. +

+ +

+Note: The simplest use of this function is to encode a string into it's +Base64 transfer content encoding. Notice the extra parenthesis around the +call to mime.b64, to discard the second return value. +

+ +
+print((mime.b64("diego:password")))
+--> ZGllZ286cGFzc3dvcmQ=
+
+

diff --git a/etc/eol.lua b/etc/eol.lua index d3da776..01dba67 100644 --- a/etc/eol.lua +++ b/etc/eol.lua @@ -1,3 +1,9 @@ +----------------------------------------------------------------------------- +-- Little program to adjust end of line markers. +-- LuaSocket sample files +-- Author: Diego Nehab +-- RCS ID: $Id$ +----------------------------------------------------------------------------- local mime = require("mime") local ltn12 = require("ltn12") local marker = '\n' diff --git a/etc/tftp.lua b/etc/tftp.lua index 6a1a093..2fc914e 100644 --- a/etc/tftp.lua +++ b/etc/tftp.lua @@ -12,11 +12,6 @@ local socket = require("socket") local ltn12 = require("ltn12") local url = require("url") ------------------------------------------------------------------------------ --- Setup namespace ------------------------------------------------------------------------------ -_LOADED["tftp"] = getfenv(1) - ----------------------------------------------------------------------------- -- Program constants ----------------------------------------------------------------------------- diff --git a/src/ftp.lua b/src/ftp.lua index 87a1e4e..f3b8a5e 100644 --- a/src/ftp.lua +++ b/src/ftp.lua @@ -13,11 +13,6 @@ local ltn12 = require("ltn12") local url = require("url") local tp = require("tp") ------------------------------------------------------------------------------ --- Setup namespace ------------------------------------------------------------------------------ -_LOADED["ftp"] = getfenv(1) - ----------------------------------------------------------------------------- -- Program constants ----------------------------------------------------------------------------- diff --git a/src/http.lua b/src/http.lua index b341ebb..3bd4d6a 100644 --- a/src/http.lua +++ b/src/http.lua @@ -13,11 +13,6 @@ local ltn12 = require("ltn12") local mime = require("mime") local url = require("url") ------------------------------------------------------------------------------ --- Setup namespace -------------------------------------------------------------------------------- -_LOADED["http"] = getfenv(1) - ----------------------------------------------------------------------------- -- Program constants ----------------------------------------------------------------------------- diff --git a/src/ltn12.lua b/src/ltn12.lua index 09771d4..04656cb 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua @@ -5,11 +5,6 @@ -- RCS ID: $Id$ ----------------------------------------------------------------------------- ------------------------------------------------------------------------------ --- Setup namespace ------------------------------------------------------------------------------ -_LOADED["ltn12"] = getfenv(1) - filter = {} source = {} sink = {} diff --git a/src/mime.c b/src/mime.c index 7810580..5b1d5e8 100644 --- a/src/mime.c +++ b/src/mime.c @@ -28,7 +28,9 @@ static int mime_global_qp(lua_State *L); static int mime_global_unqp(lua_State *L); static int mime_global_qpwrp(lua_State *L); static int mime_global_eol(lua_State *L); +static int mime_global_dot(lua_State *L); +static size_t dot(int c, size_t state, luaL_Buffer *buffer); static void b64setup(UC *b64unbase); static size_t b64encode(UC c, UC *input, size_t size, luaL_Buffer *buffer); static size_t b64pad(const UC *input, size_t size, luaL_Buffer *buffer); @@ -43,6 +45,7 @@ static size_t qppad(UC *input, size_t size, luaL_Buffer *buffer); /* code support functions */ static luaL_reg func[] = { + { "dot", mime_global_dot }, { "b64", mime_global_b64 }, { "eol", mime_global_eol }, { "qp", mime_global_qp }, @@ -659,3 +662,48 @@ static int mime_global_eol(lua_State *L) lua_pushnumber(L, ctx); return 2; } + +/*-------------------------------------------------------------------------*\ +* Takes one byte and stuff it if needed. +\*-------------------------------------------------------------------------*/ +static size_t dot(int c, size_t state, luaL_Buffer *buffer) +{ + luaL_putchar(buffer, c); + switch (c) { + case '\r': + return 1; + case '\n': + return (state == 1)? 2: 0; + case '.': + if (state == 2) + luaL_putchar(buffer, '.'); + default: + return 0; + } +} + +/*-------------------------------------------------------------------------*\ +* Incrementally applies smtp stuffing to a string +* A, n = dot(l, D) +\*-------------------------------------------------------------------------*/ +static int mime_global_dot(lua_State *L) +{ + size_t isize = 0, state = (size_t) luaL_checknumber(L, 1); + const char *input = luaL_optlstring(L, 2, NULL, &isize); + const char *last = input + isize; + luaL_Buffer buffer; + /* end-of-input blackhole */ + if (!input) { + lua_pushnil(L); + lua_pushnumber(L, 2); + return 2; + } + /* process all input */ + luaL_buffinit(L, &buffer); + while (input < last) + state = dot(*input++, state, &buffer); + luaL_pushresult(&buffer); + lua_pushnumber(L, state); + return 2; +} + diff --git a/src/mime.lua b/src/mime.lua index 4fade3b..345bd25 100644 --- a/src/mime.lua +++ b/src/mime.lua @@ -11,11 +11,6 @@ local mime = requirelib("mime", "luaopen_mime", getfenv(1)) local ltn12 = require("ltn12") ------------------------------------------------------------------------------ --- Setup namespace ------------------------------------------------------------------------------ -_LOADED["mime"] = mime - -- encode, decode and wrap algorithm tables encodet = {} decodet = {} @@ -73,3 +68,8 @@ wrap = choose(wrapt) function normalize(marker) return ltn12.filter.cycle(eol, 0, marker) end + +-- high level stuffing filter +function stuff() + return ltn12.filter.cycle(dot, 2) +end diff --git a/src/smtp.lua b/src/smtp.lua index 2ea6097..1708053 100644 --- a/src/smtp.lua +++ b/src/smtp.lua @@ -8,17 +8,11 @@ ----------------------------------------------------------------------------- -- Load required modules ----------------------------------------------------------------------------- -local smtp = requirelib("smtp", "luaopen_smtp", getfenv(1)) local socket = require("socket") local ltn12 = require("ltn12") local mime = require("mime") local tp = require("tp") ------------------------------------------------------------------------------ --- Setup namespace ------------------------------------------------------------------------------ -_LOADED["smtp"] = smtp - -- timeout for connection TIMEOUT = 60 -- default server used to send e-mails @@ -31,11 +25,6 @@ DOMAIN = os.getenv("SERVER_NAME") or "localhost" -- default time zone (means we don't know) ZONE = "-0000" --- high level stuffing filter -function stuff() - return ltn12.filter.cycle(dot, 2) -end - --------------------------------------------------------------------------- -- Low level SMTP API ----------------------------------------------------------------------------- @@ -110,7 +99,7 @@ function metat.__index:send(mailt) else self:rcpt(mailt.rcpt) end - self:data(ltn12.source.chain(mailt.source, stuff()), mailt.step) + self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step) end function open(server, port) diff --git a/src/tp.lua b/src/tp.lua index 731191e..7b581b9 100644 --- a/src/tp.lua +++ b/src/tp.lua @@ -11,11 +11,6 @@ local socket = require("socket") local ltn12 = require("ltn12") ------------------------------------------------------------------------------ --- Setup namespace ------------------------------------------------------------------------------ -_LOADED["tp"] = getfenv(1) - ----------------------------------------------------------------------------- -- Program constants ----------------------------------------------------------------------------- diff --git a/src/url.lua b/src/url.lua index c708e19..3fd0aa6 100644 --- a/src/url.lua +++ b/src/url.lua @@ -5,11 +5,6 @@ -- RCS ID: $Id$ ----------------------------------------------------------------------------- ------------------------------------------------------------------------------ --- Setup namespace ------------------------------------------------------------------------------ -_LOADED["url"] = getfenv(1) - ----------------------------------------------------------------------------- -- Encodes a string into its escaped hexadecimal representation -- Input diff --git a/test/httptest.lua b/test/httptest.lua index a171dd9..45d7e8d 100644 --- a/test/httptest.lua +++ b/test/httptest.lua @@ -390,7 +390,7 @@ local r, c, h = http.request { method = "HEAD", url = "http://www.cs.princeton.edu/~diego/" } -assert(r and h and c == 200) +assert(r and h and (c == 200), c) print("ok") ------------------------------------------------------------------------