mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-11-14 09:18:21 +01:00
d46f7a09a7
Adjusted tftp module. Added some comments.
76 lines
2.2 KiB
Lua
76 lines
2.2 KiB
Lua
-----------------------------------------------------------------------------
|
|
-- MIME support for the Lua language.
|
|
-- Author: Diego Nehab
|
|
-- Conforming to RFCs 2045-2049
|
|
-- RCS ID: $Id$
|
|
-----------------------------------------------------------------------------
|
|
|
|
-----------------------------------------------------------------------------
|
|
-- Load other required modules
|
|
-----------------------------------------------------------------------------
|
|
local mime = requirelib("mime", "luaopen_mime", getfenv(1))
|
|
local ltn12 = require("ltn12")
|
|
|
|
-----------------------------------------------------------------------------
|
|
-- Setup namespace
|
|
-----------------------------------------------------------------------------
|
|
_LOADED["mime"] = mime
|
|
|
|
-- encode, decode and wrap algorithm tables
|
|
encodet = {}
|
|
decodet = {}
|
|
wrapt = {}
|
|
|
|
-- creates a function that chooses a filter by name from a given table
|
|
local function choose(table)
|
|
return function(name, opt1, opt2)
|
|
if type(name) ~= "string" then
|
|
name, opt1, opt2 = "default", name, opt1
|
|
end
|
|
local f = table[name or "nil"]
|
|
if not f then error("unknown key (" .. tostring(name) .. ")", 3)
|
|
else return f(opt1, opt2) end
|
|
end
|
|
end
|
|
|
|
-- define the encoding filters
|
|
encodet['base64'] = function()
|
|
return ltn12.filter.cycle(b64, "")
|
|
end
|
|
|
|
encodet['quoted-printable'] = function(mode)
|
|
return ltn12.filter.cycle(qp, "",
|
|
(mode == "binary") and "=0D=0A" or "\r\n")
|
|
end
|
|
|
|
-- define the decoding filters
|
|
decodet['base64'] = function()
|
|
return ltn12.filter.cycle(unb64, "")
|
|
end
|
|
|
|
decodet['quoted-printable'] = function()
|
|
return ltn12.filter.cycle(unqp, "")
|
|
end
|
|
|
|
-- define the line-wrap filters
|
|
wrapt['text'] = function(length)
|
|
length = length or 76
|
|
return ltn12.filter.cycle(wrp, length, length)
|
|
end
|
|
wrapt['base64'] = wrapt['text']
|
|
wrapt['default'] = wrapt['text']
|
|
|
|
wrapt['quoted-printable'] = function()
|
|
return ltn12.filter.cycle(qpwrp, 76, 76)
|
|
end
|
|
|
|
-- function that choose the encoding, decoding or wrap algorithm
|
|
encode = choose(encodet)
|
|
decode = choose(decodet)
|
|
wrap = choose(wrapt)
|
|
|
|
-- define the end-of-line normalization filter
|
|
function normalize(marker)
|
|
return ltn12.filter.cycle(eol, 0, marker)
|
|
end
|