Skip to content

Instantly share code, notes, and snippets.

@allex
Created October 31, 2023 01:20
Show Gist options
  • Save allex/12babbd17917e892658e4312226275d8 to your computer and use it in GitHub Desktop.
Save allex/12babbd17917e892658e4312226275d8 to your computer and use it in GitHub Desktop.
--[[ ngx-helper.lua
author: Allex Wang (https://iallex.com/)
## ngx helper
get_var_names() - get variable names
## tools
```sh
cat <<'EOF' > get-ngx-var-names.sh
#!/bin/bash
# by allex_wang
SH_DIR="$(cd -P -- "$(dirname -- "$(readlink -f "$0")")" && pwd -P)"
curl -s https://nginx.org/en/docs/varindex.html| sed 's#</a>#\n#g' |sed -e '1,/<div id="content">/d' |grep '>\$' |sed 's#.*>\$#\$#g' |sort |uniq > "$SH_DIR/.ngx_var_names"
EOF
```
GistID: 12babbd17917e892658e4312226275d8
GistURL: https://gist.github.com/12babbd17917e892658e4312226275d8
--]]
local _M = {
-- cache the full ngx var keys (avoid load keys file duplicate
_var_name_cache = nil
}
function _M.get_var_names()
if _M._var_name_cache == nil then
_M._var_name_cache = {
"http_cookie",
"http_host",
"http_origin",
"http_purge_cache",
"http_referer",
"http_user_agent",
"http_via",
"http_x_allex",
"http_x_forwarded_for",
}
local var_key_file = ngx.config.prefix() .. "lua/.ngx_var_names"
local file = io.open(var_key_file, "r")
print("Start to load: " .. var_key_file)
if file then
-- Read the file line by line
for line in file:lines() do
table.insert(_M._var_name_cache, line)
end
file:close()
else
print("Failed to open the file")
end
end
return _M._var_name_cache
end
return _M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment