aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-17 06:23:13 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-17 06:23:13 +0000
commit613534c79526e313d6bfd5ffa514797b364dc185 (patch)
treec46efc0b1d147258ae0c65a4615e872f1de5edbe
parent94b97bdc0039c78a213e0a6170f3037eeb00122e (diff)
downloadluasocket-613534c79526e313d6bfd5ffa514797b364dc185.tar.gz
luasocket-613534c79526e313d6bfd5ffa514797b364dc185.tar.bz2
luasocket-613534c79526e313d6bfd5ffa514797b364dc185.zip
smtp.c was eatten by mime.c
-rw-r--r--TODO2
-rw-r--r--doc/mime.html29
-rw-r--r--etc/eol.lua6
-rw-r--r--etc/tftp.lua5
-rw-r--r--src/ftp.lua5
-rw-r--r--src/http.lua5
-rw-r--r--src/ltn12.lua5
-rw-r--r--src/mime.c48
-rw-r--r--src/mime.lua10
-rw-r--r--src/smtp.lua13
-rw-r--r--src/tp.lua5
-rw-r--r--src/url.lua5
-rw-r--r--test/httptest.lua2
13 files changed, 92 insertions, 48 deletions
diff --git a/TODO b/TODO
index 33e9a43..817d471 100644
--- a/TODO
+++ b/TODO
@@ -1,6 +1,8 @@
1
1ajeitar os README.* 2ajeitar os README.*
2ajeitar as referencias a RFCS e LTNS em todos os arquivos. 3ajeitar as referencias a RFCS e LTNS em todos os arquivos.
3 4
5smtp.o goes to mime.dll
4make sure sockets are closed when exceptions are raised 6make sure sockets are closed when exceptions are raised
5 7
6check garbage collection in test*.lua 8check 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")))
230--&gt; ZGllZ286cGFzc3dvcmQ= 230--&gt; ZGllZ286cGFzc3dvcmQ=
231</pre> 231</pre>
232 232
233<!-- dot +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
234<p class=name id="dot">
235A, B = mime.<b>dot(</b>C [, D]<b>)</b>
236</p>
237
238<p class=description>
239Low-level filter to perform Base64 encoding.
240</p>
241
242<p class=description>
243<tt>A</tt> is the encoded version of the largest prefix of
244<tt>C..D</tt>
245that can be encoded unambiguously. <tt>B</tt> has the remaining bytes of
246<tt>C..D</tt>, <em>before</em> encoding.
247If <tt>D</tt> is <tt><b>nil</b></tt>, <tt>A</tt> is padded with
248the encoding of the remaining bytes of <tt>C</tt>.
249</p>
250
251<p class=note>
252Note: The simplest use of this function is to encode a string into it's
253Base64 transfer content encoding. Notice the extra parenthesis around the
254call to <tt>mime.b64</tt>, to discard the second return value.
255</p>
256
257<pre class=example>
258print((mime.b64("diego:password")))
259--&gt; ZGllZ286cGFzc3dvcmQ=
260</pre>
261
233<!-- eol ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> 262<!-- eol ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
234 263
235<p class=name id="eol"> 264<p class=name id="eol">
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 @@
1-----------------------------------------------------------------------------
2-- Little program to adjust end of line markers.
3-- LuaSocket sample files
4-- Author: Diego Nehab
5-- RCS ID: $Id$
6-----------------------------------------------------------------------------
1local mime = require("mime") 7local mime = require("mime")
2local ltn12 = require("ltn12") 8local ltn12 = require("ltn12")
3local marker = '\n' 9local marker = '\n'
diff --git a/etc/tftp.lua b/etc/tftp.lua
index 6a1a093..2fc914e 100644
--- a/etc/tftp.lua
+++ b/etc/tftp.lua
@@ -13,11 +13,6 @@ local ltn12 = require("ltn12")
13local url = require("url") 13local url = require("url")
14 14
15----------------------------------------------------------------------------- 15-----------------------------------------------------------------------------
16-- Setup namespace
17-----------------------------------------------------------------------------
18_LOADED["tftp"] = getfenv(1)
19
20-----------------------------------------------------------------------------
21-- Program constants 16-- Program constants
22----------------------------------------------------------------------------- 17-----------------------------------------------------------------------------
23local char = string.char 18local char = string.char
diff --git a/src/ftp.lua b/src/ftp.lua
index 87a1e4e..f3b8a5e 100644
--- a/src/ftp.lua
+++ b/src/ftp.lua
@@ -14,11 +14,6 @@ local url = require("url")
14local tp = require("tp") 14local tp = require("tp")
15 15
16----------------------------------------------------------------------------- 16-----------------------------------------------------------------------------
17-- Setup namespace
18-----------------------------------------------------------------------------
19_LOADED["ftp"] = getfenv(1)
20
21-----------------------------------------------------------------------------
22-- Program constants 17-- Program constants
23----------------------------------------------------------------------------- 18-----------------------------------------------------------------------------
24-- timeout in seconds before the program gives up on a connection 19-- timeout in seconds before the program gives up on a connection
diff --git a/src/http.lua b/src/http.lua
index b341ebb..3bd4d6a 100644
--- a/src/http.lua
+++ b/src/http.lua
@@ -14,11 +14,6 @@ local mime = require("mime")
14local url = require("url") 14local url = require("url")
15 15
16----------------------------------------------------------------------------- 16-----------------------------------------------------------------------------
17-- Setup namespace
18-------------------------------------------------------------------------------
19_LOADED["http"] = getfenv(1)
20
21-----------------------------------------------------------------------------
22-- Program constants 17-- Program constants
23----------------------------------------------------------------------------- 18-----------------------------------------------------------------------------
24-- connection timeout in seconds 19-- connection timeout in seconds
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 @@
5-- RCS ID: $Id$ 5-- RCS ID: $Id$
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7 7
8-----------------------------------------------------------------------------
9-- Setup namespace
10-----------------------------------------------------------------------------
11_LOADED["ltn12"] = getfenv(1)
12
13filter = {} 8filter = {}
14source = {} 9source = {}
15sink = {} 10sink = {}
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);
28static int mime_global_unqp(lua_State *L); 28static int mime_global_unqp(lua_State *L);
29static int mime_global_qpwrp(lua_State *L); 29static int mime_global_qpwrp(lua_State *L);
30static int mime_global_eol(lua_State *L); 30static int mime_global_eol(lua_State *L);
31static int mime_global_dot(lua_State *L);
31 32
33static size_t dot(int c, size_t state, luaL_Buffer *buffer);
32static void b64setup(UC *b64unbase); 34static void b64setup(UC *b64unbase);
33static size_t b64encode(UC c, UC *input, size_t size, luaL_Buffer *buffer); 35static size_t b64encode(UC c, UC *input, size_t size, luaL_Buffer *buffer);
34static size_t b64pad(const UC *input, size_t size, luaL_Buffer *buffer); 36static 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);
43 45
44/* code support functions */ 46/* code support functions */
45static luaL_reg func[] = { 47static luaL_reg func[] = {
48 { "dot", mime_global_dot },
46 { "b64", mime_global_b64 }, 49 { "b64", mime_global_b64 },
47 { "eol", mime_global_eol }, 50 { "eol", mime_global_eol },
48 { "qp", mime_global_qp }, 51 { "qp", mime_global_qp },
@@ -659,3 +662,48 @@ static int mime_global_eol(lua_State *L)
659 lua_pushnumber(L, ctx); 662 lua_pushnumber(L, ctx);
660 return 2; 663 return 2;
661} 664}
665
666/*-------------------------------------------------------------------------*\
667* Takes one byte and stuff it if needed.
668\*-------------------------------------------------------------------------*/
669static size_t dot(int c, size_t state, luaL_Buffer *buffer)
670{
671 luaL_putchar(buffer, c);
672 switch (c) {
673 case '\r':
674 return 1;
675 case '\n':
676 return (state == 1)? 2: 0;
677 case '.':
678 if (state == 2)
679 luaL_putchar(buffer, '.');
680 default:
681 return 0;
682 }
683}
684
685/*-------------------------------------------------------------------------*\
686* Incrementally applies smtp stuffing to a string
687* A, n = dot(l, D)
688\*-------------------------------------------------------------------------*/
689static int mime_global_dot(lua_State *L)
690{
691 size_t isize = 0, state = (size_t) luaL_checknumber(L, 1);
692 const char *input = luaL_optlstring(L, 2, NULL, &isize);
693 const char *last = input + isize;
694 luaL_Buffer buffer;
695 /* end-of-input blackhole */
696 if (!input) {
697 lua_pushnil(L);
698 lua_pushnumber(L, 2);
699 return 2;
700 }
701 /* process all input */
702 luaL_buffinit(L, &buffer);
703 while (input < last)
704 state = dot(*input++, state, &buffer);
705 luaL_pushresult(&buffer);
706 lua_pushnumber(L, state);
707 return 2;
708}
709
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 @@
11local mime = requirelib("mime", "luaopen_mime", getfenv(1)) 11local mime = requirelib("mime", "luaopen_mime", getfenv(1))
12local ltn12 = require("ltn12") 12local ltn12 = require("ltn12")
13 13
14-----------------------------------------------------------------------------
15-- Setup namespace
16-----------------------------------------------------------------------------
17_LOADED["mime"] = mime
18
19-- encode, decode and wrap algorithm tables 14-- encode, decode and wrap algorithm tables
20encodet = {} 15encodet = {}
21decodet = {} 16decodet = {}
@@ -73,3 +68,8 @@ wrap = choose(wrapt)
73function normalize(marker) 68function normalize(marker)
74 return ltn12.filter.cycle(eol, 0, marker) 69 return ltn12.filter.cycle(eol, 0, marker)
75end 70end
71
72-- high level stuffing filter
73function stuff()
74 return ltn12.filter.cycle(dot, 2)
75end
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 @@
8----------------------------------------------------------------------------- 8-----------------------------------------------------------------------------
9-- Load required modules 9-- Load required modules
10----------------------------------------------------------------------------- 10-----------------------------------------------------------------------------
11local smtp = requirelib("smtp", "luaopen_smtp", getfenv(1))
12local socket = require("socket") 11local socket = require("socket")
13local ltn12 = require("ltn12") 12local ltn12 = require("ltn12")
14local mime = require("mime") 13local mime = require("mime")
15local tp = require("tp") 14local tp = require("tp")
16 15
17-----------------------------------------------------------------------------
18-- Setup namespace
19-----------------------------------------------------------------------------
20_LOADED["smtp"] = smtp
21
22-- timeout for connection 16-- timeout for connection
23TIMEOUT = 60 17TIMEOUT = 60
24-- default server used to send e-mails 18-- default server used to send e-mails
@@ -31,11 +25,6 @@ DOMAIN = os.getenv("SERVER_NAME") or "localhost"
31-- default time zone (means we don't know) 25-- default time zone (means we don't know)
32ZONE = "-0000" 26ZONE = "-0000"
33 27
34-- high level stuffing filter
35function stuff()
36 return ltn12.filter.cycle(dot, 2)
37end
38
39--------------------------------------------------------------------------- 28---------------------------------------------------------------------------
40-- Low level SMTP API 29-- Low level SMTP API
41----------------------------------------------------------------------------- 30-----------------------------------------------------------------------------
@@ -110,7 +99,7 @@ function metat.__index:send(mailt)
110 else 99 else
111 self:rcpt(mailt.rcpt) 100 self:rcpt(mailt.rcpt)
112 end 101 end
113 self:data(ltn12.source.chain(mailt.source, stuff()), mailt.step) 102 self:data(ltn12.source.chain(mailt.source, mime.stuff()), mailt.step)
114end 103end
115 104
116function open(server, port) 105function 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
@@ -12,11 +12,6 @@ local socket = require("socket")
12local ltn12 = require("ltn12") 12local ltn12 = require("ltn12")
13 13
14----------------------------------------------------------------------------- 14-----------------------------------------------------------------------------
15-- Setup namespace
16-----------------------------------------------------------------------------
17_LOADED["tp"] = getfenv(1)
18
19-----------------------------------------------------------------------------
20-- Program constants 15-- Program constants
21----------------------------------------------------------------------------- 16-----------------------------------------------------------------------------
22TIMEOUT = 60 17TIMEOUT = 60
diff --git a/src/url.lua b/src/url.lua
index c708e19..3fd0aa6 100644
--- a/src/url.lua
+++ b/src/url.lua
@@ -6,11 +6,6 @@
6----------------------------------------------------------------------------- 6-----------------------------------------------------------------------------
7 7
8----------------------------------------------------------------------------- 8-----------------------------------------------------------------------------
9-- Setup namespace
10-----------------------------------------------------------------------------
11_LOADED["url"] = getfenv(1)
12
13-----------------------------------------------------------------------------
14-- Encodes a string into its escaped hexadecimal representation 9-- Encodes a string into its escaped hexadecimal representation
15-- Input 10-- Input
16-- s: binary string to be encoded 11-- s: binary string to be encoded
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 {
390 method = "HEAD", 390 method = "HEAD",
391 url = "http://www.cs.princeton.edu/~diego/" 391 url = "http://www.cs.princeton.edu/~diego/"
392} 392}
393assert(r and h and c == 200) 393assert(r and h and (c == 200), c)
394print("ok") 394print("ok")
395 395
396------------------------------------------------------------------------ 396------------------------------------------------------------------------