diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-01-21 01:09:50 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-01-21 01:09:50 +0000 |
commit | 3a7ac1e04361e12ddfcbf344e9e1db82fb88157b (patch) | |
tree | 166d452836a474541e1054f77b0b586bbe68dd6e /src/mime.lua | |
parent | 0b61b577f5d65a9c8bd5e690c4010c1e28b70e66 (diff) | |
download | luasocket-3a7ac1e04361e12ddfcbf344e9e1db82fb88157b.tar.gz luasocket-3a7ac1e04361e12ddfcbf344e9e1db82fb88157b.tar.bz2 luasocket-3a7ac1e04361e12ddfcbf344e9e1db82fb88157b.zip |
Changed the naming convention of the mime module.
Looks beautiful.
Diffstat (limited to 'src/mime.lua')
-rw-r--r-- | src/mime.lua | 92 |
1 files changed, 44 insertions, 48 deletions
diff --git a/src/mime.lua b/src/mime.lua index 86b3af2..0251f6e 100644 --- a/src/mime.lua +++ b/src/mime.lua | |||
@@ -10,75 +10,71 @@ socket.mime = mime | |||
10 | setmetatable(mime, { __index = _G }) | 10 | setmetatable(mime, { __index = _G }) |
11 | setfenv(1, mime) | 11 | setfenv(1, mime) |
12 | 12 | ||
13 | base64 = {} | 13 | -- encode, decode and wrap algorithm tables |
14 | qprint = {} | 14 | local et = {} |
15 | local dt = {} | ||
16 | local wt = {} | ||
15 | 17 | ||
16 | function base64.encode() | 18 | -- creates a function that chooses an algorithm from a given table |
17 | local unfinished = "" | 19 | local function choose(table) |
18 | return function(chunk) | 20 | return function(method, ...) |
19 | local done | 21 | local f = table[method or "nil"] |
20 | done, unfinished = b64(unfinished, chunk) | 22 | if not f then return nil, "unknown method (" .. tostring(method) .. ")" |
21 | return done | 23 | else return f(unpack(arg)) end |
22 | end | 24 | end |
23 | end | 25 | end |
24 | 26 | ||
25 | function base64.decode() | 27 | -- creates a function that cicles a filter with a given initial |
26 | local unfinished = "" | 28 | -- context and extra arguments |
29 | local function cicle(f, ctx, ...) | ||
27 | return function(chunk) | 30 | return function(chunk) |
28 | local done | 31 | local ret |
29 | done, unfinished = unb64(unfinished, chunk) | 32 | ret, ctx = f(ctx, chunk, unpack(arg)) |
30 | return done | 33 | return ret |
31 | end | 34 | end |
32 | end | 35 | end |
33 | 36 | ||
34 | function qprint.encode(mode) | 37 | -- function that choose the encoding, decoding or wrap algorithm |
35 | mode = (mode == "binary") and "=0D=0A" or "\13\10" | 38 | encode = choose(et) |
36 | local unfinished = "" | 39 | decode = choose(dt) |
37 | return function(chunk) | 40 | wrap = choose(wt) |
38 | local done | 41 | |
39 | done, unfinished = qp(unfinished, chunk, mode) | 42 | -- define the encoding algorithms |
40 | return done | 43 | et['base64'] = function() |
41 | end | 44 | return cicle(b64, "") |
42 | end | 45 | end |
43 | 46 | ||
44 | function qprint.decode() | 47 | et['quoted-printable'] = function(mode) |
45 | local unfinished = "" | 48 | return cicle(qp, "", (mode == "binary") and "=0D=0A" or "\13\10") |
46 | return function(chunk) | 49 | end |
47 | local done | 50 | |
48 | done, unfinished = unqp(unfinished, chunk) | 51 | -- define the decoding algorithms |
49 | return done | 52 | dt['base64'] = function() |
50 | end | 53 | return cicle(unb64, "") |
51 | end | 54 | end |
52 | 55 | ||
53 | function split(length, marker) | 56 | dt['quoted-printable'] = function() |
57 | return cicle(unqp, "") | ||
58 | end | ||
59 | |||
60 | -- define the wrap algorithms | ||
61 | wt['character'] = function(length) | ||
54 | length = length or 76 | 62 | length = length or 76 |
55 | local left = length | 63 | return cicle(fmt, length, length) |
56 | return function(chunk) | ||
57 | local done | ||
58 | done, left = fmt(chunk, length, left, marker) | ||
59 | return done | ||
60 | end | ||
61 | end | 64 | end |
65 | wt['base64'] = wt['character'] | ||
62 | 66 | ||
63 | function qprint.split(length) | 67 | wt['quoted-printable'] = function(length) |
64 | length = length or 76 | 68 | length = length or 76 |
65 | local left = length | 69 | return cicle(qpfmt, length, length) |
66 | return function(chunk) | ||
67 | local done | ||
68 | done, left = qpfmt(chunk, length, left) | ||
69 | return done | ||
70 | end | ||
71 | end | 70 | end |
72 | 71 | ||
72 | -- define the end-of-line translation function | ||
73 | function canonic(marker) | 73 | function canonic(marker) |
74 | local unfinished = "" | 74 | return cicle(eol, "", marker) |
75 | return function(chunk) | ||
76 | local done | ||
77 | done, unfinished = eol(unfinished, chunk, marker) | ||
78 | return done | ||
79 | end | ||
80 | end | 75 | end |
81 | 76 | ||
77 | -- chains several filters together | ||
82 | function chain(...) | 78 | function chain(...) |
83 | local layers = table.getn(arg) | 79 | local layers = table.getn(arg) |
84 | return function (chunk) | 80 | return function (chunk) |