aboutsummaryrefslogtreecommitdiff
path: root/src/mime.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-02-11 03:31:53 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-02-11 03:31:53 +0000
commit390846b640ee7013d51a766b4b2472bdcfbbdfcc (patch)
tree3bedd023a0a84feb714615245b58eab5ffb4309b /src/mime.lua
parent0b2542d1a61fc5425ff65ab3dbf7ba7de174763f (diff)
downloadluasocket-390846b640ee7013d51a766b4b2472bdcfbbdfcc.tar.gz
luasocket-390846b640ee7013d51a766b4b2472bdcfbbdfcc.tar.bz2
luasocket-390846b640ee7013d51a766b4b2472bdcfbbdfcc.zip
Added ltn12 module. Modified mime to be stand alone.
Still crashes on invalid input. Dunno why.
Diffstat (limited to 'src/mime.lua')
-rw-r--r--src/mime.lua74
1 files changed, 25 insertions, 49 deletions
diff --git a/src/mime.lua b/src/mime.lua
index 369567f..4df0388 100644
--- a/src/mime.lua
+++ b/src/mime.lua
@@ -1,11 +1,6 @@
1-- make sure LuaSocket is loaded 1if not ltn12 then error('This module requires LTN12') end
2if not LUASOCKET_LIBNAME then error('module requires LuaSocket') end 2-- create mime namespace
3-- get LuaSocket namespace 3mime = mime or {}
4local socket = _G[LUASOCKET_LIBNAME]
5if not socket then error('module requires LuaSocket') end
6-- create code namespace inside LuaSocket namespace
7local mime = socket.mime or {}
8socket.mime = mime
9-- make all module globals fall into mime namespace 4-- make all module globals fall into mime namespace
10setmetatable(mime, { __index = _G }) 5setmetatable(mime, { __index = _G })
11setfenv(1, mime) 6setfenv(1, mime)
@@ -15,80 +10,61 @@ local et = {}
15local dt = {} 10local dt = {}
16local wt = {} 11local wt = {}
17 12
18-- creates a function that chooses a filter from a given table 13-- creates a function that chooses a filter by name from a given table
19local function choose(table) 14local function choose(table)
20 return function(filter, ...) 15 return function(name, opt)
21 local f = table[filter or "nil"] 16 local f = table[name or "nil"]
22 if not f then error("unknown filter (" .. tostring(filter) .. ")", 3) 17 if not f then error("unknown filter (" .. tostring(name) .. ")", 3)
23 else return f(unpack(arg)) end 18 else return f(opt) end
24 end 19 end
25end 20end
26 21
27-- define the encoding filters 22-- define the encoding filters
28et['base64'] = function() 23et['base64'] = function()
29 return socket.cicle(b64, "") 24 return ltn12.filter.cycle(b64, "")
30end 25end
31 26
32et['quoted-printable'] = function(mode) 27et['quoted-printable'] = function(mode)
33 return socket.cicle(qp, "", (mode == "binary") and "=0D=0A" or "\13\10") 28 return ltn12.filter.cycle(qp, "",
29 (mode == "binary") and "=0D=0A" or "\13\10")
34end 30end
35 31
36-- define the decoding filters 32-- define the decoding filters
37dt['base64'] = function() 33dt['base64'] = function()
38 return socket.cicle(unb64, "") 34 return ltn12.filter.cycle(unb64, "")
39end 35end
40 36
41dt['quoted-printable'] = function() 37dt['quoted-printable'] = function()
42 return socket.cicle(unqp, "") 38 return ltn12.filter.cycle(unqp, "")
43end 39end
44 40
45-- define the line-wrap filters 41-- define the line-wrap filters
46wt['text'] = function(length) 42wt['text'] = function(length)
47 length = length or 76 43 length = length or 76
48 return socket.cicle(wrp, length, length) 44 return ltn12.filter.cycle(wrp, length, length)
49end 45end
50wt['base64'] = wt['text'] 46wt['base64'] = wt['text']
51 47
52wt['quoted-printable'] = function() 48wt['quoted-printable'] = function()
53 return socket.cicle(qpwrp, 76, 76) 49 return ltn12.filter.cycle(qpwrp, 76, 76)
54end 50end
55 51
56-- function that choose the encoding, decoding or wrap algorithm 52-- function that choose the encoding, decoding or wrap algorithm
57encode = choose(et) 53encode = choose(et)
58decode = choose(dt) 54decode = choose(dt)
59-- there is a default wrap filter 55-- there is different because there is a default wrap filter
60local cwt = choose(wt) 56local cwt = choose(wt)
61function wrap(...) 57function wrap(mode_or_length, length)
62 if type(arg[1]) ~= "string" then table.insert(arg, 1, "text") end 58 if type(mode_or_length) ~= "string" then
63 return cwt(unpack(arg)) 59 length = mode_or_length
64end 60 mode_or_length = "text"
65 61 end
66-- define the end-of-line translation filter 62 return cwt(mode_or_length, length)
67function canonic(marker)
68 return socket.cicle(eol, "", marker)
69end 63end
70 64
71-- chains several filters together 65-- define the end-of-line normalization filter
72function chain(...) 66function normalize(marker)
73 local layers = table.getn(arg) 67 return ltn12.filter.cycle(eol, 0, marker)
74 return function (chunk)
75 if not chunk then
76 local parts = {}
77 for i = 1, layers do
78 for j = i, layers do
79 chunk = arg[j](chunk)
80 end
81 table.insert(parts, chunk)
82 chunk = nil
83 end
84 return table.concat(parts)
85 else
86 for j = 1, layers do
87 chunk = arg[j](chunk)
88 end
89 return chunk
90 end
91 end
92end 68end
93 69
94return mime 70return mime