-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathutils.lua
More file actions
136 lines (105 loc) · 2.8 KB
/
utils.lua
File metadata and controls
136 lines (105 loc) · 2.8 KB
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
-- https://github.com/api7/lua-resty-http
local http = require("resty.http")
local clear_tab = require("table.clear")
local split = require("ngx.re").split
local find = ngx.re.find
local concat_tab = table.concat
local tostring = tostring
local select = select
local ipairs = ipairs
local pairs = pairs
local type = type
local str_char = string.char
if not http.tls_handshake then
error("Bad http library. Should use api7-lua-resty-http instead")
end
local _M = {http = http}
local normalize
do
local items = {}
local function concat(sep, ...)
local argc = select('#', ...)
clear_tab(items)
local len = 0
for i = 1, argc do
local v = select(i, ...)
if v ~= nil then
len = len + 1
items[len] = tostring(v)
end
end
return concat_tab(items, sep);
end
local segs = {}
function normalize(...)
local path = concat('/', ...)
local names = {}
local err
segs, err = split(path, [[/]], "jo", nil, nil, segs)
if not segs then
return nil, err
end
local len = 0
for _, seg in ipairs(segs) do
if seg == '..' then
if len > 0 then
len = len - 1
end
elseif seg == '' or seg == '/' and names[len] == '/' then
-- do nothing
elseif seg ~= '.' then
len = len + 1
names[len] = seg
end
end
return '/' .. concat_tab(names, '/', 1, len);
end
end
_M.normalize = normalize
function _M.get_real_key(prefix, key)
return (type(prefix) == 'string' and prefix or "") .. key
end
function _M.has_value(arr, val)
for key, value in pairs(arr) do
if value == val then
return key
end
end
return false
end
function _M.starts_with(str, start)
return str:sub(1, #start) == start
end
local ngx_log = ngx.log
local ngx_ERR = ngx.ERR
local ngx_INFO = ngx.INFO
local ngx_WARN = ngx.WARN
local function log_error(...)
return ngx_log(ngx_ERR, ...)
end
_M.log_error = log_error
local function log_info( ... )
return ngx_log(ngx_INFO, ...)
end
_M.log_info = log_info
local function log_warn( ... )
return ngx_log(ngx_WARN, ...)
end
_M.log_warn = log_warn
local function verify_key(key)
if not key or #key == 0 then
return false, "key should not be empty"
end
return true, nil
end
_M.verify_key = verify_key
local function is_empty_str(input_str)
return (find(input_str or '', [=[^\s*$]=], "jo"))
end
_M.is_empty_str = is_empty_str
--- @param key string
--- @return string
_M.next_key = function(key)
return key .. str_char(0)
end
return _M