diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-02-11 03:31:53 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-02-11 03:31:53 +0000 |
| commit | 390846b640ee7013d51a766b4b2472bdcfbbdfcc (patch) | |
| tree | 3bedd023a0a84feb714615245b58eab5ffb4309b /src/mime.lua | |
| parent | 0b2542d1a61fc5425ff65ab3dbf7ba7de174763f (diff) | |
| download | luasocket-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.lua | 74 |
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 | 1 | if not ltn12 then error('This module requires LTN12') end |
| 2 | if not LUASOCKET_LIBNAME then error('module requires LuaSocket') end | 2 | -- create mime namespace |
| 3 | -- get LuaSocket namespace | 3 | mime = mime or {} |
| 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 | 4 | -- make all module globals fall into mime namespace |
| 10 | setmetatable(mime, { __index = _G }) | 5 | setmetatable(mime, { __index = _G }) |
| 11 | setfenv(1, mime) | 6 | setfenv(1, mime) |
| @@ -15,80 +10,61 @@ local et = {} | |||
| 15 | local dt = {} | 10 | local dt = {} |
| 16 | local wt = {} | 11 | local 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 |
| 19 | local function choose(table) | 14 | local 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 |
| 25 | end | 20 | end |
| 26 | 21 | ||
| 27 | -- define the encoding filters | 22 | -- define the encoding filters |
| 28 | et['base64'] = function() | 23 | et['base64'] = function() |
| 29 | return socket.cicle(b64, "") | 24 | return ltn12.filter.cycle(b64, "") |
| 30 | end | 25 | end |
| 31 | 26 | ||
| 32 | et['quoted-printable'] = function(mode) | 27 | et['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") | ||
| 34 | end | 30 | end |
| 35 | 31 | ||
| 36 | -- define the decoding filters | 32 | -- define the decoding filters |
| 37 | dt['base64'] = function() | 33 | dt['base64'] = function() |
| 38 | return socket.cicle(unb64, "") | 34 | return ltn12.filter.cycle(unb64, "") |
| 39 | end | 35 | end |
| 40 | 36 | ||
| 41 | dt['quoted-printable'] = function() | 37 | dt['quoted-printable'] = function() |
| 42 | return socket.cicle(unqp, "") | 38 | return ltn12.filter.cycle(unqp, "") |
| 43 | end | 39 | end |
| 44 | 40 | ||
| 45 | -- define the line-wrap filters | 41 | -- define the line-wrap filters |
| 46 | wt['text'] = function(length) | 42 | wt['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) |
| 49 | end | 45 | end |
| 50 | wt['base64'] = wt['text'] | 46 | wt['base64'] = wt['text'] |
| 51 | 47 | ||
| 52 | wt['quoted-printable'] = function() | 48 | wt['quoted-printable'] = function() |
| 53 | return socket.cicle(qpwrp, 76, 76) | 49 | return ltn12.filter.cycle(qpwrp, 76, 76) |
| 54 | end | 50 | end |
| 55 | 51 | ||
| 56 | -- function that choose the encoding, decoding or wrap algorithm | 52 | -- function that choose the encoding, decoding or wrap algorithm |
| 57 | encode = choose(et) | 53 | encode = choose(et) |
| 58 | decode = choose(dt) | 54 | decode = choose(dt) |
| 59 | -- there is a default wrap filter | 55 | -- there is different because there is a default wrap filter |
| 60 | local cwt = choose(wt) | 56 | local cwt = choose(wt) |
| 61 | function wrap(...) | 57 | function 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 |
| 64 | end | 60 | mode_or_length = "text" |
| 65 | 61 | end | |
| 66 | -- define the end-of-line translation filter | 62 | return cwt(mode_or_length, length) |
| 67 | function canonic(marker) | ||
| 68 | return socket.cicle(eol, "", marker) | ||
| 69 | end | 63 | end |
| 70 | 64 | ||
| 71 | -- chains several filters together | 65 | -- define the end-of-line normalization filter |
| 72 | function chain(...) | 66 | function 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 | ||
| 92 | end | 68 | end |
| 93 | 69 | ||
| 94 | return mime | 70 | return mime |
