/usr/share/lua/5.1/http/headers.lua is in lua-http 0.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | --[[
HTTP Header data structure/type
Design criteria:
- the same header field is allowed more than once
- must be able to fetch seperate occurences (important for some headers e.g. Set-Cookie)
- optionally available as comma seperated list
- http2 adds flag to headers that they should never be indexed
- header order should be recoverable
I chose to implement headers as an array of entries.
An index of field name => array indices is kept.
]]
local unpack = table.unpack or unpack -- luacheck: ignore 113
local entry_methods = {}
local entry_mt = {
__name = "http.headers.entry";
__index = entry_methods;
}
local never_index_defaults = {
authorization = true;
["proxy-authorization"] = true;
cookie = true;
["set-cookie"] = true;
}
local function new_entry(name, value, never_index)
if never_index == nil then
never_index = never_index_defaults[name] or false
end
return setmetatable({
name = name;
value = value;
never_index = never_index;
}, entry_mt)
end
function entry_methods:modify(value, never_index)
self.value = value
if never_index == nil then
never_index = never_index_defaults[self.name] or false
end
self.never_index = never_index
end
function entry_methods:unpack()
return self.name, self.value, self.never_index
end
function entry_methods:clone()
return new_entry(self.name, self.value, self.never_index)
end
local headers_methods = {}
local headers_mt = {
__name = "http.headers";
__index = headers_methods;
}
local function new_headers()
return setmetatable({
_n = 0;
_data = {};
_index = {};
}, headers_mt)
end
function headers_methods:len()
return self._n
end
headers_mt.__len = headers_methods.len
function headers_mt:__tostring()
return string.format("http.headers{%d headers}", self._n)
end
local function add_to_index(_index, name, i)
local dex = _index[name]
if dex == nil then
dex = {n=1, i}
_index[name] = dex
else
local n = dex.n + 1
dex[n] = i
dex.n = n
end
end
local function rebuild_index(self)
local index = {}
for i=1, self._n do
local entry = self._data[i]
add_to_index(index, entry.name, i)
end
self._index = index
end
function headers_methods:clone()
local index, new_data = {}, {}
for i=1, self._n do
local entry = self._data[i]
new_data[i] = entry:clone()
add_to_index(index, entry.name, i)
end
return setmetatable({
_n = self._n;
_data = new_data;
_index = index;
}, headers_mt)
end
function headers_methods:append(name, ...)
local n = self._n + 1
self._data[n] = new_entry(name, ...)
add_to_index(self._index, name, n)
self._n = n
end
function headers_methods:each()
local i = 0
return function(self) -- luacheck: ignore 432
if i >= self._n then return end
i = i + 1
local entry = self._data[i]
return entry:unpack()
end, self
end
headers_mt.__pairs = headers_methods.each
function headers_methods:has(name)
local dex = self._index[name]
return dex ~= nil
end
function headers_methods:delete(name)
local dex = self._index[name]
if dex then
local n = dex.n
for i=n, 1, -1 do
table.remove(self._data, dex[i])
end
self._n = self._n - n
rebuild_index(self)
return true
else
return false
end
end
function headers_methods:geti(i)
local e = self._data[i]
if e == nil then return nil end
return e:unpack()
end
function headers_methods:get_as_sequence(name)
local dex = self._index[name]
if dex == nil then return { n = 0; } end
local r = { n = dex.n; }
for i=1, r.n do
r[i] = self._data[dex[i]].value
end
return r
end
function headers_methods:get(name)
local r = self:get_as_sequence(name)
return unpack(r, 1, r.n)
end
function headers_methods:get_comma_separated(name)
local r = self:get_as_sequence(name)
if r.n == 0 then
return nil
else
return table.concat(r, ",", 1, r.n)
end
end
function headers_methods:modifyi(i, ...)
local e = self._data[i]
if e == nil then error("invalid index") end
e:modify(...)
end
function headers_methods:upsert(name, ...)
local dex = self._index[name]
if dex == nil then
self:append(name, ...)
else
assert(dex[2] == nil, "Cannot upsert multi-valued field")
self:modifyi(dex[1], ...)
end
end
local function default_cmp(a, b)
if a.name ~= b.name then
-- Things with a colon *must* be before others
local a_is_colon = a.name:sub(1,1) == ":"
local b_is_colon = b.name:sub(1,1) == ":"
if a_is_colon and not b_is_colon then
return true
elseif not a_is_colon and b_is_colon then
return false
else
return a.name < b.name
end
end
if a.value ~= b.value then
return a.value < b.value
end
return a.never_index
end
function headers_methods:sort()
table.sort(self._data, default_cmp)
rebuild_index(self)
end
function headers_methods:dump(file, prefix)
file = file or io.stderr
prefix = prefix or ""
for name, value in self:each() do
assert(file:write(string.format("%s%s: %s\n", prefix, name, value)))
end
assert(file:flush())
end
return {
new = new_headers;
methods = headers_methods;
mt = headers_mt;
}
|