aboutsummaryrefslogtreecommitdiff
path: root/src/mime.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/mime.lua')
-rw-r--r--src/mime.lua104
1 files changed, 104 insertions, 0 deletions
diff --git a/src/mime.lua b/src/mime.lua
new file mode 100644
index 0000000..86b3af2
--- /dev/null
+++ b/src/mime.lua
@@ -0,0 +1,104 @@
1-- make sure LuaSocket is loaded
2if not LUASOCKET_LIBNAME then error('module requires LuaSocket') end
3-- get LuaSocket namespace
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
10setmetatable(mime, { __index = _G })
11setfenv(1, mime)
12
13base64 = {}
14qprint = {}
15
16function base64.encode()
17 local unfinished = ""
18 return function(chunk)
19 local done
20 done, unfinished = b64(unfinished, chunk)
21 return done
22 end
23end
24
25function base64.decode()
26 local unfinished = ""
27 return function(chunk)
28 local done
29 done, unfinished = unb64(unfinished, chunk)
30 return done
31 end
32end
33
34function qprint.encode(mode)
35 mode = (mode == "binary") and "=0D=0A" or "\13\10"
36 local unfinished = ""
37 return function(chunk)
38 local done
39 done, unfinished = qp(unfinished, chunk, mode)
40 return done
41 end
42end
43
44function qprint.decode()
45 local unfinished = ""
46 return function(chunk)
47 local done
48 done, unfinished = unqp(unfinished, chunk)
49 return done
50 end
51end
52
53function split(length, marker)
54 length = length or 76
55 local left = length
56 return function(chunk)
57 local done
58 done, left = fmt(chunk, length, left, marker)
59 return done
60 end
61end
62
63function qprint.split(length)
64 length = length or 76
65 local left = length
66 return function(chunk)
67 local done
68 done, left = qpfmt(chunk, length, left)
69 return done
70 end
71end
72
73function canonic(marker)
74 local unfinished = ""
75 return function(chunk)
76 local done
77 done, unfinished = eol(unfinished, chunk, marker)
78 return done
79 end
80end
81
82function chain(...)
83 local layers = table.getn(arg)
84 return function (chunk)
85 if not chunk then
86 local parts = {}
87 for i = 1, layers do
88 for j = i, layers do
89 chunk = arg[j](chunk)
90 end
91 table.insert(parts, chunk)
92 chunk = nil
93 end
94 return table.concat(parts)
95 else
96 for j = 1, layers do
97 chunk = arg[j](chunk)
98 end
99 return chunk
100 end
101 end
102end
103
104return code