2001-09-12 20:27:40 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- URI parsing, composition and relative URL resolution
|
2003-06-26 20:47:49 +02:00
|
|
|
-- LuaSocket toolkit.
|
2001-09-12 20:27:40 +02:00
|
|
|
-- Author: Diego Nehab
|
2004-06-04 17:15:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2004-05-30 23:36:22 +02:00
|
|
|
|
2004-06-04 17:15:45 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2004-09-27 06:01:18 +02:00
|
|
|
-- Declare module
|
|
|
|
-----------------------------------------------------------------------------
|
2004-11-27 08:58:04 +01:00
|
|
|
local string = require("string")
|
2005-06-14 06:29:23 +02:00
|
|
|
local base = _G
|
2004-11-27 08:58:04 +01:00
|
|
|
local table = require("table")
|
2013-05-27 10:45:09 +02:00
|
|
|
local socket = require("socket")
|
|
|
|
|
|
|
|
socket.url = {}
|
|
|
|
local _M = socket.url
|
2004-09-27 06:01:18 +02:00
|
|
|
|
2005-09-29 08:11:42 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Module version
|
|
|
|
-----------------------------------------------------------------------------
|
2013-06-11 13:10:03 +02:00
|
|
|
_M._VERSION = "URL 1.0.3"
|
2005-09-29 08:11:42 +02:00
|
|
|
|
2004-09-27 06:01:18 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2004-01-19 06:41:30 +01:00
|
|
|
-- Encodes a string into its escaped hexadecimal representation
|
2005-11-22 09:33:29 +01:00
|
|
|
-- Input
|
2004-01-19 06:41:30 +01:00
|
|
|
-- s: binary string to be encoded
|
|
|
|
-- Returns
|
|
|
|
-- escaped representation of string binary
|
|
|
|
-----------------------------------------------------------------------------
|
2013-05-27 10:45:09 +02:00
|
|
|
function _M.escape(s)
|
2011-05-12 14:44:16 +02:00
|
|
|
return (string.gsub(s, "([^A-Za-z0-9_])", function(c)
|
2004-01-19 06:41:30 +01:00
|
|
|
return string.format("%%%02x", string.byte(c))
|
2011-05-12 14:44:16 +02:00
|
|
|
end))
|
2004-01-19 06:41:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Protects a path segment, to prevent it from interfering with the
|
2005-11-22 09:33:29 +01:00
|
|
|
-- url parsing.
|
|
|
|
-- Input
|
2004-01-19 06:41:30 +01:00
|
|
|
-- s: binary string to be encoded
|
|
|
|
-- Returns
|
|
|
|
-- escaped representation of string binary
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
local function make_set(t)
|
2011-05-25 22:57:22 +02:00
|
|
|
local s = {}
|
|
|
|
for i,v in base.ipairs(t) do
|
|
|
|
s[t[i]] = 1
|
|
|
|
end
|
|
|
|
return s
|
2004-01-19 06:41:30 +01:00
|
|
|
end
|
|
|
|
|
2017-03-16 17:53:02 +01:00
|
|
|
-- these are allowed within a path segment, along with alphanum
|
2004-01-19 06:41:30 +01:00
|
|
|
-- other characters must be escaped
|
|
|
|
local segment_set = make_set {
|
2005-11-22 09:33:29 +01:00
|
|
|
"-", "_", ".", "!", "~", "*", "'", "(",
|
2011-05-25 22:57:22 +02:00
|
|
|
")", ":", "@", "&", "=", "+", "$", ",",
|
2004-01-19 06:41:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
local function protect_segment(s)
|
2011-05-25 22:57:22 +02:00
|
|
|
return string.gsub(s, "([^A-Za-z0-9_])", function (c)
|
|
|
|
if segment_set[c] then return c
|
2017-03-16 16:33:28 +01:00
|
|
|
else return string.format("%%%02X", string.byte(c)) end
|
2011-05-25 22:57:22 +02:00
|
|
|
end)
|
2004-01-19 06:41:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
2016-04-12 01:01:51 +02:00
|
|
|
-- Unencodes a escaped hexadecimal string into its binary representation
|
2005-11-22 09:33:29 +01:00
|
|
|
-- Input
|
2016-04-12 01:01:51 +02:00
|
|
|
-- s: escaped hexadecimal string to be unencoded
|
2004-01-19 06:41:30 +01:00
|
|
|
-- Returns
|
2016-04-12 01:01:51 +02:00
|
|
|
-- unescaped binary representation of escaped hexadecimal binary
|
2004-01-19 06:41:30 +01:00
|
|
|
-----------------------------------------------------------------------------
|
2013-05-27 10:45:09 +02:00
|
|
|
function _M.unescape(s)
|
2011-05-12 14:44:16 +02:00
|
|
|
return (string.gsub(s, "%%(%x%x)", function(hex)
|
2004-11-27 08:58:04 +01:00
|
|
|
return string.char(base.tonumber(hex, 16))
|
2011-05-12 14:44:16 +02:00
|
|
|
end))
|
2004-01-19 06:41:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Builds a path from a base path and a relative path
|
|
|
|
-- Input
|
|
|
|
-- base_path
|
|
|
|
-- relative_path
|
|
|
|
-- Returns
|
|
|
|
-- corresponding absolute path
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
local function absolute_path(base_path, relative_path)
|
|
|
|
if string.sub(relative_path, 1, 1) == "/" then return relative_path end
|
|
|
|
local path = string.gsub(base_path, "[^/]*$", "")
|
|
|
|
path = path .. relative_path
|
2005-11-22 09:33:29 +01:00
|
|
|
path = string.gsub(path, "([^/]*%./)", function (s)
|
2004-01-19 06:41:30 +01:00
|
|
|
if s ~= "./" then return s else return "" end
|
|
|
|
end)
|
|
|
|
path = string.gsub(path, "/%.$", "/")
|
2005-11-22 09:33:29 +01:00
|
|
|
local reduced
|
2004-01-19 06:41:30 +01:00
|
|
|
while reduced ~= path do
|
|
|
|
reduced = path
|
|
|
|
path = string.gsub(reduced, "([^/]*/%.%./)", function (s)
|
|
|
|
if s ~= "../../" then return "" else return s end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
path = string.gsub(reduced, "([^/]*/%.%.)$", function (s)
|
|
|
|
if s ~= "../.." then return "" else return s end
|
|
|
|
end)
|
|
|
|
return path
|
|
|
|
end
|
2001-09-12 20:27:40 +02:00
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Parses a url and returns a table with all its parts according to RFC 2396
|
|
|
|
-- The following grammar describes the names given to the URL parts
|
|
|
|
-- <url> ::= <scheme>://<authority>/<path>;<params>?<query>#<fragment>
|
|
|
|
-- <authority> ::= <userinfo>@<host>:<port>
|
|
|
|
-- <userinfo> ::= <user>[:<password>]
|
|
|
|
-- <path> :: = {<segment>/}<segment>
|
|
|
|
-- Input
|
|
|
|
-- url: uniform resource locator of request
|
|
|
|
-- default: table with default values for each field
|
|
|
|
-- Returns
|
|
|
|
-- table with the following fields, where RFC naming conventions have
|
|
|
|
-- been preserved:
|
2005-11-22 09:33:29 +01:00
|
|
|
-- scheme, authority, userinfo, user, password, host, port,
|
2001-09-12 20:27:40 +02:00
|
|
|
-- path, params, query, fragment
|
|
|
|
-- Obs:
|
|
|
|
-- the leading '/' in {/<path>} is considered part of <path>
|
|
|
|
-----------------------------------------------------------------------------
|
2013-05-27 10:45:09 +02:00
|
|
|
function _M.parse(url, default)
|
2001-09-12 20:27:40 +02:00
|
|
|
-- initialize default parameters
|
2004-06-17 00:51:04 +02:00
|
|
|
local parsed = {}
|
2005-06-13 00:02:21 +02:00
|
|
|
for i,v in base.pairs(default or parsed) do parsed[i] = v end
|
2001-09-12 20:27:40 +02:00
|
|
|
-- empty url is parsed to nil
|
2004-05-28 08:16:43 +02:00
|
|
|
if not url or url == "" then return nil, "invalid url" end
|
2001-09-12 20:27:40 +02:00
|
|
|
-- remove whitespace
|
2004-03-26 01:18:41 +01:00
|
|
|
-- url = string.gsub(url, "%s", "")
|
2001-09-12 20:27:40 +02:00
|
|
|
-- get fragment
|
2005-11-22 09:33:29 +01:00
|
|
|
url = string.gsub(url, "#(.*)$", function(f)
|
|
|
|
parsed.fragment = f
|
2005-11-20 08:20:26 +01:00
|
|
|
return ""
|
|
|
|
end)
|
2001-09-12 20:27:40 +02:00
|
|
|
-- get scheme
|
2005-11-22 09:33:29 +01:00
|
|
|
url = string.gsub(url, "^([%w][%w%+%-%.]*)%:",
|
2005-11-20 08:20:26 +01:00
|
|
|
function(s) parsed.scheme = s; return "" end)
|
2001-09-12 20:27:40 +02:00
|
|
|
-- get authority
|
2005-11-22 09:33:29 +01:00
|
|
|
url = string.gsub(url, "^//([^/]*)", function(n)
|
|
|
|
parsed.authority = n
|
2005-11-20 08:20:26 +01:00
|
|
|
return ""
|
|
|
|
end)
|
2012-04-22 18:18:45 +02:00
|
|
|
-- get query string
|
2005-11-22 09:33:29 +01:00
|
|
|
url = string.gsub(url, "%?(.*)", function(q)
|
|
|
|
parsed.query = q
|
2005-11-20 08:20:26 +01:00
|
|
|
return ""
|
|
|
|
end)
|
2001-09-12 20:27:40 +02:00
|
|
|
-- get params
|
2005-11-22 09:33:29 +01:00
|
|
|
url = string.gsub(url, "%;(.*)", function(p)
|
|
|
|
parsed.params = p
|
2005-11-20 08:20:26 +01:00
|
|
|
return ""
|
|
|
|
end)
|
2004-03-26 01:18:41 +01:00
|
|
|
-- path is whatever was left
|
2001-09-12 20:27:40 +02:00
|
|
|
if url ~= "" then parsed.path = url end
|
|
|
|
local authority = parsed.authority
|
|
|
|
if not authority then return parsed end
|
2002-12-03 00:34:41 +01:00
|
|
|
authority = string.gsub(authority,"^([^@]*)@",
|
2005-11-20 08:20:26 +01:00
|
|
|
function(u) parsed.userinfo = u; return "" end)
|
2012-04-22 18:18:45 +02:00
|
|
|
authority = string.gsub(authority, ":([^:%]]*)$",
|
2005-11-20 08:20:26 +01:00
|
|
|
function(p) parsed.port = p; return "" end)
|
2012-04-22 18:18:45 +02:00
|
|
|
if authority ~= "" then
|
|
|
|
-- IPv6?
|
|
|
|
parsed.host = string.match(authority, "^%[(.+)%]$") or authority
|
|
|
|
end
|
2001-09-12 20:27:40 +02:00
|
|
|
local userinfo = parsed.userinfo
|
|
|
|
if not userinfo then return parsed end
|
2005-11-22 09:33:29 +01:00
|
|
|
userinfo = string.gsub(userinfo, ":([^:]*)$",
|
2005-11-20 08:20:26 +01:00
|
|
|
function(p) parsed.password = p; return "" end)
|
2005-11-22 09:33:29 +01:00
|
|
|
parsed.user = userinfo
|
2001-09-12 20:27:40 +02:00
|
|
|
return parsed
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Rebuilds a parsed URL from its components.
|
|
|
|
-- Components are protected if any reserved or unallowed characters are found
|
|
|
|
-- Input
|
2004-01-19 06:41:30 +01:00
|
|
|
-- parsed: parsed URL, as returned by parse
|
2001-09-12 20:27:40 +02:00
|
|
|
-- Returns
|
2002-12-03 00:34:41 +01:00
|
|
|
-- a stringing with the corresponding URL
|
2001-09-12 20:27:40 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2013-05-27 10:45:09 +02:00
|
|
|
function _M.build(parsed)
|
2017-03-20 20:56:15 +01:00
|
|
|
--local ppath = _M.parse_path(parsed.path or "")
|
|
|
|
--local url = _M.build_path(ppath)
|
|
|
|
local url = parsed.path or ""
|
2001-09-12 20:27:40 +02:00
|
|
|
if parsed.params then url = url .. ";" .. parsed.params end
|
|
|
|
if parsed.query then url = url .. "?" .. parsed.query end
|
2011-05-25 22:57:22 +02:00
|
|
|
local authority = parsed.authority
|
|
|
|
if parsed.host then
|
|
|
|
authority = parsed.host
|
2012-04-22 18:18:45 +02:00
|
|
|
if string.find(authority, ":") then -- IPv6?
|
|
|
|
authority = "[" .. authority .. "]"
|
|
|
|
end
|
2017-03-16 16:57:17 +01:00
|
|
|
if parsed.port then authority = authority .. ":" .. base.tostring(parsed.port) end
|
2011-05-25 22:57:22 +02:00
|
|
|
local userinfo = parsed.userinfo
|
|
|
|
if parsed.user then
|
|
|
|
userinfo = parsed.user
|
|
|
|
if parsed.password then
|
|
|
|
userinfo = userinfo .. ":" .. parsed.password
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if userinfo then authority = userinfo .. "@" .. authority end
|
|
|
|
end
|
2012-04-11 22:21:25 +02:00
|
|
|
if authority then url = "//" .. authority .. url end
|
2001-09-12 20:27:40 +02:00
|
|
|
if parsed.scheme then url = parsed.scheme .. ":" .. url end
|
|
|
|
if parsed.fragment then url = url .. "#" .. parsed.fragment end
|
2004-03-26 01:18:41 +01:00
|
|
|
-- url = string.gsub(url, "%s", "")
|
2001-09-27 22:05:30 +02:00
|
|
|
return url
|
2001-09-12 20:27:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Builds a absolute URL from a base and a relative URL according to RFC 2396
|
|
|
|
-- Input
|
|
|
|
-- base_url
|
|
|
|
-- relative_url
|
|
|
|
-- Returns
|
|
|
|
-- corresponding absolute url
|
|
|
|
-----------------------------------------------------------------------------
|
2013-05-27 10:45:09 +02:00
|
|
|
function _M.absolute(base_url, relative_url)
|
2016-02-11 13:54:59 +01:00
|
|
|
local base_parsed
|
2005-02-27 19:30:32 +01:00
|
|
|
if base.type(base_url) == "table" then
|
2012-04-11 22:21:25 +02:00
|
|
|
base_parsed = base_url
|
2013-05-27 10:45:09 +02:00
|
|
|
base_url = _M.build(base_parsed)
|
2001-09-12 20:27:40 +02:00
|
|
|
else
|
2013-05-27 10:45:09 +02:00
|
|
|
base_parsed = _M.parse(base_url)
|
2005-02-27 19:30:32 +01:00
|
|
|
end
|
2013-05-27 10:45:09 +02:00
|
|
|
local relative_parsed = _M.parse(relative_url)
|
2005-02-27 19:30:32 +01:00
|
|
|
if not base_parsed then return relative_url
|
|
|
|
elseif not relative_parsed then return base_url
|
|
|
|
elseif relative_parsed.scheme then return relative_url
|
|
|
|
else
|
|
|
|
relative_parsed.scheme = base_parsed.scheme
|
|
|
|
if not relative_parsed.authority then
|
|
|
|
relative_parsed.authority = base_parsed.authority
|
|
|
|
if not relative_parsed.path then
|
|
|
|
relative_parsed.path = base_parsed.path
|
|
|
|
if not relative_parsed.params then
|
|
|
|
relative_parsed.params = base_parsed.params
|
|
|
|
if not relative_parsed.query then
|
|
|
|
relative_parsed.query = base_parsed.query
|
2001-09-12 20:27:40 +02:00
|
|
|
end
|
|
|
|
end
|
2005-11-22 09:33:29 +01:00
|
|
|
else
|
|
|
|
relative_parsed.path = absolute_path(base_parsed.path or "",
|
2005-02-27 19:30:32 +01:00
|
|
|
relative_parsed.path)
|
2001-09-12 20:27:40 +02:00
|
|
|
end
|
|
|
|
end
|
2013-05-27 10:45:09 +02:00
|
|
|
return _M.build(relative_parsed)
|
2001-09-12 20:27:40 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Breaks a path into its segments, unescaping the segments
|
|
|
|
-- Input
|
|
|
|
-- path
|
|
|
|
-- Returns
|
|
|
|
-- segment: a table with one entry per segment
|
|
|
|
-----------------------------------------------------------------------------
|
2013-05-27 10:45:09 +02:00
|
|
|
function _M.parse_path(path)
|
2011-05-25 22:57:22 +02:00
|
|
|
local parsed = {}
|
|
|
|
path = path or ""
|
|
|
|
--path = string.gsub(path, "%s", "")
|
|
|
|
string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end)
|
2013-02-25 23:28:28 +01:00
|
|
|
for i = 1, #parsed do
|
2013-05-27 10:45:09 +02:00
|
|
|
parsed[i] = _M.unescape(parsed[i])
|
2011-05-25 22:57:22 +02:00
|
|
|
end
|
|
|
|
if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end
|
|
|
|
if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end
|
|
|
|
return parsed
|
2001-09-12 20:27:40 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Builds a path component from its segments, escaping protected characters.
|
|
|
|
-- Input
|
|
|
|
-- parsed: path segments
|
2001-09-26 22:38:47 +02:00
|
|
|
-- unsafe: if true, segments are not protected before path is built
|
2001-09-12 20:27:40 +02:00
|
|
|
-- Returns
|
2004-01-19 06:41:30 +01:00
|
|
|
-- path: corresponding path stringing
|
2001-09-12 20:27:40 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2013-05-27 10:45:09 +02:00
|
|
|
function _M.build_path(parsed, unsafe)
|
2011-05-25 22:57:22 +02:00
|
|
|
local path = ""
|
2013-02-25 23:28:28 +01:00
|
|
|
local n = #parsed
|
2011-05-25 22:57:22 +02:00
|
|
|
if unsafe then
|
|
|
|
for i = 1, n-1 do
|
|
|
|
path = path .. parsed[i]
|
|
|
|
path = path .. "/"
|
|
|
|
end
|
|
|
|
if n > 0 then
|
|
|
|
path = path .. parsed[n]
|
|
|
|
if parsed.is_directory then path = path .. "/" end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for i = 1, n-1 do
|
|
|
|
path = path .. protect_segment(parsed[i])
|
|
|
|
path = path .. "/"
|
|
|
|
end
|
|
|
|
if n > 0 then
|
|
|
|
path = path .. protect_segment(parsed[n])
|
|
|
|
if parsed.is_directory then path = path .. "/" end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if parsed.is_absolute then path = "/" .. path end
|
|
|
|
return path
|
2001-09-12 20:27:40 +02:00
|
|
|
end
|
2013-05-27 10:45:09 +02:00
|
|
|
|
2013-06-11 13:10:03 +02:00
|
|
|
return _M
|