Repository navigation
Expand file tree
/
Copy pathuri.lua
More file actions
152 lines (119 loc) · 3.46 KB
/
Copy pathuri.lua
File metadata and controls
152 lines (119 loc) · 3.46 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
local _M = {}
local string_char = string.char
local string_upper = string.upper
local string_find = string.find
local string_sub = string.sub
local string_byte = string.byte
local string_format = string.format
local tonumber = tonumber
local table_concat = table.concat
local ngx_re_gsub = ngx.re.gsub
local RESERVED_CHARACTERS = {
[0x21] = true, -- !
[0x23] = true, -- #
[0x24] = true, -- $
[0x25] = true, -- %
[0x26] = true, -- &
[0x27] = true, -- '
[0x28] = true, -- (
[0x29] = true, -- )
[0x2A] = true, -- *
[0x2B] = true, -- +
[0x2C] = true, -- ,
[0x2F] = true, -- /
[0x3A] = true, -- :
[0x3B] = true, -- ;
[0x3D] = true, -- =
[0x3F] = true, -- ?
[0x40] = true, -- @
[0x5B] = true, -- [
[0x5D] = true, -- ]
}
local TMP_OUTPUT = require("table.new")(16, 0)
local DOT = string_byte(".")
local SLASH = string_byte("/")
local function percent_decode(m)
local hex = m[1]
local num = tonumber(hex, 16)
if RESERVED_CHARACTERS[num] then
return string_upper(m[0])
end
return string_char(num)
end
local function escape(m)
return string_format("%%%02X", string_byte(m[0]))
end
function _M.normalize(uri, merge_slashes)
-- check for simple cases and early exit
if uri == "" or uri == "/" then
return uri
end
-- check if uri needs to be percent-decoded
-- (this can in some cases lead to unnecessary percent-decoding)
if string_find(uri, "%", 1, true) then
-- decoding percent-encoded triplets of unreserved characters
uri = ngx_re_gsub(uri, "%([\\dA-F]{2})", percent_decode, "joi")
end
-- check if the uri contains a dot
-- (this can in some cases lead to unnecessary dot removal processing)
if string_find(uri, ".", 1, true) == nil then
if not merge_slashes then
return uri
end
if string_find(uri, "//", 1, true) == nil then
return uri
end
end
local output_n = 0
while #uri > 0 do
local FIRST = string_byte(uri, 1)
local SECOND = FIRST and string_byte(uri, 2) or nil
local THIRD = SECOND and string_byte(uri, 3) or nil
local FOURTH = THIRD and string_byte(uri, 4) or nil
if uri == "/." then -- /.
uri = "/"
elseif uri == "/.." then -- /..
uri = "/"
if output_n > 0 then
output_n = output_n - 1
end
elseif uri == "." or uri == ".." then
uri = ""
elseif FIRST == DOT and SECOND == DOT and THIRD == SLASH then -- ../
uri = string_sub(uri, 4)
elseif FIRST == DOT and SECOND == SLASH then -- ./
uri = string_sub(uri, 3)
elseif FIRST == SLASH and SECOND == DOT and THIRD == SLASH then -- /./
uri = string_sub(uri, 3)
elseif FIRST == SLASH and SECOND == DOT and THIRD == DOT and FOURTH == SLASH then -- /../
uri = string_sub(uri, 4)
if output_n > 0 then
output_n = output_n - 1
end
elseif merge_slashes and FIRST == SLASH and SECOND == SLASH then -- //
uri = string_sub(uri, 2)
else
local i = string_find(uri, "/", 2, true)
output_n = output_n + 1
if i then
local seg = string_sub(uri, 1, i - 1)
TMP_OUTPUT[output_n] = seg
uri = string_sub(uri, i)
else
TMP_OUTPUT[output_n] = uri
uri = ""
end
end
end
if output_n == 0 then
return ""
end
if output_n == 1 then
return TMP_OUTPUT[1]
end
return table_concat(TMP_OUTPUT, nil, 1, output_n)
end
function _M.escape(uri)
return ngx_re_gsub(uri, "[^!#$&'()*+,/:;=?@[\\]A-Z\\d-_.~%]", escape, "joi")
end
return _M