diff options
Diffstat (limited to 'src/mime.lua')
-rw-r--r-- | src/mime.lua | 104 |
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 | ||
2 | if not LUASOCKET_LIBNAME then error('module requires LuaSocket') end | ||
3 | -- get LuaSocket namespace | ||
4 | local socket = _G[LUASOCKET_LIBNAME] | ||
5 | if not socket then error('module requires LuaSocket') end | ||
6 | -- create code namespace inside LuaSocket namespace | ||
7 | local mime = socket.mime or {} | ||
8 | socket.mime = mime | ||
9 | -- make all module globals fall into mime namespace | ||
10 | setmetatable(mime, { __index = _G }) | ||
11 | setfenv(1, mime) | ||
12 | |||
13 | base64 = {} | ||
14 | qprint = {} | ||
15 | |||
16 | function base64.encode() | ||
17 | local unfinished = "" | ||
18 | return function(chunk) | ||
19 | local done | ||
20 | done, unfinished = b64(unfinished, chunk) | ||
21 | return done | ||
22 | end | ||
23 | end | ||
24 | |||
25 | function base64.decode() | ||
26 | local unfinished = "" | ||
27 | return function(chunk) | ||
28 | local done | ||
29 | done, unfinished = unb64(unfinished, chunk) | ||
30 | return done | ||
31 | end | ||
32 | end | ||
33 | |||
34 | function 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 | ||
42 | end | ||
43 | |||
44 | function qprint.decode() | ||
45 | local unfinished = "" | ||
46 | return function(chunk) | ||
47 | local done | ||
48 | done, unfinished = unqp(unfinished, chunk) | ||
49 | return done | ||
50 | end | ||
51 | end | ||
52 | |||
53 | function 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 | ||
61 | end | ||
62 | |||
63 | function 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 | ||
71 | end | ||
72 | |||
73 | function canonic(marker) | ||
74 | local unfinished = "" | ||
75 | return function(chunk) | ||
76 | local done | ||
77 | done, unfinished = eol(unfinished, chunk, marker) | ||
78 | return done | ||
79 | end | ||
80 | end | ||
81 | |||
82 | function 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 | ||
102 | end | ||
103 | |||
104 | return code | ||