|
|
local lpeg = require "lpeg" |
|
|
local http_patts = require "lpeg_patterns.http" |
|
|
local IPv4_patts = require "lpeg_patterns.IPv4" |
|
|
local IPv6_patts = require "lpeg_patterns.IPv6" |
|
|
|
|
|
local EOF = lpeg.P(-1) |
|
|
|
|
|
|
|
|
local function char_to_pchar(c) |
|
|
return string.format("%%%02X", c:byte(1,1)) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
local function encodeURI(str) |
|
|
return (str:gsub("[^%;%,%/%?%:%@%&%=%+%$%w%-%_%.%!%~%*%'%(%)%#]", char_to_pchar)) |
|
|
end |
|
|
|
|
|
|
|
|
local function encodeURIComponent(str) |
|
|
return (str:gsub("[^%w%-_%.%!%~%*%'%(%)]", char_to_pchar)) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
local decodeURI do |
|
|
local decodeURI_blacklist = {} |
|
|
for char in ("#$&+,/:;=?@"):gmatch(".") do |
|
|
decodeURI_blacklist[string.byte(char)] = true |
|
|
end |
|
|
local function decodeURI_helper(str) |
|
|
local x = tonumber(str, 16) |
|
|
if not decodeURI_blacklist[x] then |
|
|
return string.char(x) |
|
|
end |
|
|
|
|
|
end |
|
|
function decodeURI(str) |
|
|
return (str:gsub("%%(%x%x)", decodeURI_helper)) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
local function pchar_to_char(str) |
|
|
return string.char(tonumber(str, 16)) |
|
|
end |
|
|
|
|
|
|
|
|
local function decodeURIComponent(str) |
|
|
return (str:gsub("%%(%x%x)", pchar_to_char)) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
local function query_args(str) |
|
|
local iter, state, first = str:gmatch("([^=&]+)(=?)([^&]*)&?") |
|
|
return function(state, last) |
|
|
local name, equals, value = iter(state, last) |
|
|
if name == nil then return nil end |
|
|
name = decodeURIComponent(name) |
|
|
if equals == "" then |
|
|
value = nil |
|
|
else |
|
|
value = decodeURIComponent(value) |
|
|
end |
|
|
return name, value |
|
|
end, state, first |
|
|
end |
|
|
|
|
|
|
|
|
local function dict_to_query(form) |
|
|
local r, i = {}, 0 |
|
|
for name, value in pairs(form) do |
|
|
i = i + 1 |
|
|
r[i] = encodeURIComponent(name).."="..encodeURIComponent(value) |
|
|
end |
|
|
return table.concat(r, "&", 1, i) |
|
|
end |
|
|
|
|
|
|
|
|
local function resolve_relative_path(orig_path, relative_path) |
|
|
local t, i = {}, 0 |
|
|
|
|
|
local is_abs |
|
|
if relative_path:sub(1,1) == "/" then |
|
|
|
|
|
is_abs = true |
|
|
else |
|
|
is_abs = orig_path:sub(1,1) == "/" |
|
|
|
|
|
|
|
|
for segment in orig_path:gmatch("([^/]+)/") do |
|
|
i = i + 1 |
|
|
t[i] = segment |
|
|
end |
|
|
end |
|
|
|
|
|
for segment in relative_path:gmatch("([^/]+)") do |
|
|
if segment == ".." then |
|
|
|
|
|
if i > 0 then |
|
|
|
|
|
i = i - 1 |
|
|
end |
|
|
elseif segment ~= "." then |
|
|
i = i + 1 |
|
|
t[i] = segment |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
local s |
|
|
if is_abs then |
|
|
if i == 0 then return "/" end |
|
|
t[0] = "" |
|
|
s = 0 |
|
|
else |
|
|
s = 1 |
|
|
end |
|
|
|
|
|
if relative_path:sub(-1, -1) == "/" then |
|
|
i = i + 1 |
|
|
t[i] = "" |
|
|
end |
|
|
return table.concat(t, "/", s, i) |
|
|
end |
|
|
|
|
|
local safe_methods = { |
|
|
|
|
|
|
|
|
|
|
|
GET = true; |
|
|
HEAD = true; |
|
|
OPTIONS = true; |
|
|
TRACE = true; |
|
|
} |
|
|
local function is_safe_method(method) |
|
|
return safe_methods[method] or false |
|
|
end |
|
|
|
|
|
local IPaddress = (IPv4_patts.IPv4address + IPv6_patts.IPv6addrz) * EOF |
|
|
local function is_ip(str) |
|
|
return IPaddress:match(str) ~= nil |
|
|
end |
|
|
|
|
|
local scheme_to_port = { |
|
|
http = 80; |
|
|
ws = 80; |
|
|
https = 443; |
|
|
wss = 443; |
|
|
} |
|
|
|
|
|
|
|
|
local function split_authority(authority, scheme) |
|
|
local host, port |
|
|
local h, p = authority:match("^[ \t]*(.-):(%d+)[ \t]*$") |
|
|
if p then |
|
|
authority = h |
|
|
port = tonumber(p, 10) |
|
|
else |
|
|
port = scheme_to_port[scheme] |
|
|
if port == nil then |
|
|
return nil, "unknown scheme" |
|
|
end |
|
|
end |
|
|
local ipv6 = authority:match("^%[([:%x]+)%]$") |
|
|
if ipv6 then |
|
|
host = ipv6 |
|
|
else |
|
|
host = authority |
|
|
end |
|
|
return host, port |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
local function to_authority(host, port, scheme) |
|
|
local authority = host |
|
|
if host:match("^[%x:]+:[%x:]*$") then |
|
|
authority = "[" .. authority .. "]" |
|
|
end |
|
|
local default_port = scheme_to_port[scheme] |
|
|
if default_port == port then |
|
|
port = nil |
|
|
end |
|
|
if port then |
|
|
authority = string.format("%s:%d", authority, port) |
|
|
end |
|
|
return authority |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
local function imf_date(time) |
|
|
return os.date("!%a, %d %b %Y %H:%M:%S GMT", time) |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
local maybe_quote do |
|
|
local patt = http_patts.token * EOF |
|
|
+ lpeg.Cs(lpeg.Cc'"' * ((lpeg.S"\\\"") / "\\%0" + http_patts.qdtext)^0 * lpeg.Cc'"') * EOF |
|
|
maybe_quote = function (s) |
|
|
return patt:match(s) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
local yieldable_pcall |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
|
package.loaded.ngx |
|
|
and type(package.loaded.jit) == "table" |
|
|
and type(package.loaded.jit.version_num) == "number" |
|
|
and package.loaded.jit.version_num >= 20000 |
|
|
) |
|
|
|
|
|
or coroutine.wrap(function() |
|
|
return pcall(coroutine.yield, true) end |
|
|
)() then |
|
|
yieldable_pcall = pcall |
|
|
else |
|
|
local function handle_resume(co, ok, ...) |
|
|
if not ok then |
|
|
return false, ... |
|
|
elseif coroutine.status(co) == "dead" then |
|
|
return true, ... |
|
|
end |
|
|
return handle_resume(co, coroutine.resume(co, coroutine.yield(...))) |
|
|
end |
|
|
yieldable_pcall = function(func, ...) |
|
|
if type(func) ~= "function" or debug.getinfo(func, "S").what == "C" then |
|
|
local C_func = func |
|
|
|
|
|
func = function(...) return C_func(...) end |
|
|
end |
|
|
local co = coroutine.create(func) |
|
|
return handle_resume(co, coroutine.resume(co, ...)) |
|
|
end |
|
|
end |
|
|
|
|
|
return { |
|
|
encodeURI = encodeURI; |
|
|
encodeURIComponent = encodeURIComponent; |
|
|
decodeURI = decodeURI; |
|
|
decodeURIComponent = decodeURIComponent; |
|
|
query_args = query_args; |
|
|
dict_to_query = dict_to_query; |
|
|
resolve_relative_path = resolve_relative_path; |
|
|
is_safe_method = is_safe_method; |
|
|
is_ip = is_ip; |
|
|
scheme_to_port = scheme_to_port; |
|
|
split_authority = split_authority; |
|
|
to_authority = to_authority; |
|
|
imf_date = imf_date; |
|
|
maybe_quote = maybe_quote; |
|
|
yieldable_pcall = yieldable_pcall; |
|
|
} |
|
|
|