aboutsummaryrefslogtreecommitdiff
path: root/src/headers.lua
blob: 004ef378d1cadb778fe27dd5dd387fed8a347b18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
-----------------------------------------------------------------------------
-- Canonic header field capitalization
-- LuaSocket toolkit.
-- Author: Diego Nehab
-----------------------------------------------------------------------------
local socket = require("socket")
socket.headers = {}
local _M = socket.headers

-- capitalizes the first letter of each hyphen-separated word, lowercases
-- the rest (e.g. "x-request-id" -> "X-Request-Id")
local function titlecase(header)
    return (header:gsub("(%a)([%w]*)", function(a, b) return a:upper()..b:lower() end))
end

_M.canonic = {}

setmetatable(_M.canonic, {
    __index = function(t, key)
        if type(key) ~= "string" then
            return nil
        end

        local lower = key:lower()
        local v = rawget(t, lower)
        if v then
            return v
        end

        v = titlecase(lower)
        rawset(t, lower, v)
        return v
    end
})

-- adds a header with a given canonical capitalization, e.g. for headers
-- whose capitalization titlecase(header) would not reproduce correctly
function _M.setcanonic(header)
    if type(header) ~= "string" then
        error("header must be a string", 2)
    end
    _M.canonic[header:lower()] = header
end

-- headers whose canonical capitalization titlecase() does not reproduce
-- (acronyms and other irregular capitalization). Anything not listed here
-- is generated and cached on first lookup by the __index above.
_M.setcanonic("Content-ID")
_M.setcanonic("Content-MD5")
_M.setcanonic("DSN-Gateway")
_M.setcanonic("ETag")
_M.setcanonic("Final-Log-ID")
_M.setcanonic("Message-ID")
_M.setcanonic("MIME-Version")
_M.setcanonic("Original-Envelope-ID")
_M.setcanonic("Received-From-MTA")
_M.setcanonic("Remote-MTA")
_M.setcanonic("Reporting-MTA")
_M.setcanonic("Resent-Message-ID")
_M.setcanonic("SMTP-Remote-Recipient")
_M.setcanonic("TE")
_M.setcanonic("WWW-Authenticate")

return _M