1 local rawget, rawset, setmetatable =
2 rawget, rawset, setmetatable
4 local str_gsub = string.gsub
5 local str_lower = string.lower
13 -- Returns an empty headers table with internalised case normalisation.
14 -- Supports the same cases as in ngx_lua:
16 -- headers.content_length
17 -- headers["content-length"]
18 -- headers["Content-Length"]
25 mt.__index = function(t, k)
26 local k_hyphened = str_gsub(k, "_", "-")
27 local matched = rawget(t, k)
31 local k_normalised = str_lower(k_hyphened)
32 return rawget(t, mt.normalised[k_normalised])
37 -- First check the normalised table. If there's no match (first time) add an entry for
38 -- our current case in the normalised table. This is to preserve the human (prettier) case
39 -- instead of outputting lowercased header names.
41 -- If there's a match, we're being updated, just with a different case for the key. We use
42 -- the normalised table to give us the original key, and perorm a rawset().
43 mt.__newindex = function(t, k, v)
44 -- we support underscore syntax, so always hyphenate.
45 local k_hyphened = str_gsub(k, "_", "-")
47 -- lowercase hyphenated is "normalised"
48 local k_normalised = str_lower(k_hyphened)
50 if not mt.normalised[k_normalised] then
51 mt.normalised[k_normalised] = k_hyphened
52 rawset(t, k_hyphened, v)
54 rawset(t, mt.normalised[k_normalised], v)
58 return setmetatable({}, mt)