2001-09-12 20:27:40 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- URI parsing, composition and relative URL resolution
|
2002-07-08 23:01:45 +02:00
|
|
|
-- LuaSocket 1.5 toolkit.
|
2001-09-12 20:27:40 +02:00
|
|
|
-- Author: Diego Nehab
|
|
|
|
-- Date: 20/7/2001
|
|
|
|
-- Conforming to: RFC 2396, LTN7
|
|
|
|
-- RCS ID: $Id$
|
|
|
|
----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local Public, Private = {}, {}
|
|
|
|
URL = Public
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- 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:
|
|
|
|
-- scheme, authority, userinfo, user, password, host, port,
|
|
|
|
-- path, params, query, fragment
|
|
|
|
-- Obs:
|
|
|
|
-- the leading '/' in {/<path>} is considered part of <path>
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Public.parse_url(url, default)
|
|
|
|
-- initialize default parameters
|
|
|
|
local parsed = default or {}
|
|
|
|
-- empty url is parsed to nil
|
|
|
|
if not url or url == "" then return nil end
|
|
|
|
-- remove whitespace
|
2002-12-03 00:34:41 +01:00
|
|
|
url = string.gsub(url, "%s", "")
|
2001-09-12 20:27:40 +02:00
|
|
|
-- get fragment
|
2002-12-03 00:34:41 +01:00
|
|
|
url = string.gsub(url, "#(.*)$", function(f) parsed.fragment = f end)
|
2001-09-12 20:27:40 +02:00
|
|
|
-- get scheme
|
2002-12-03 00:34:41 +01:00
|
|
|
url = string.gsub(url, "^([%w][%w%+%-%.]*)%:",
|
|
|
|
function(s) parsed.scheme = s end)
|
2001-09-12 20:27:40 +02:00
|
|
|
-- get authority
|
2002-12-03 00:34:41 +01:00
|
|
|
url = string.gsub(url, "^//([^/]*)", function(n) parsed.authority = n end)
|
|
|
|
-- get query stringing
|
|
|
|
url = string.gsub(url, "%?(.*)", function(q) parsed.query = q end)
|
2001-09-12 20:27:40 +02:00
|
|
|
-- get params
|
2002-12-03 00:34:41 +01:00
|
|
|
url = string.gsub(url, "%;(.*)", function(p) parsed.params = p end)
|
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,"^([^@]*)@",
|
|
|
|
function(u) parsed.userinfo = u end)
|
|
|
|
authority = string.gsub(authority, ":([^:]*)$",
|
|
|
|
function(p) parsed.port = p end)
|
2001-09-12 20:27:40 +02:00
|
|
|
if authority ~= "" then parsed.host = authority end
|
|
|
|
local userinfo = parsed.userinfo
|
|
|
|
if not userinfo then return parsed end
|
2002-12-03 00:34:41 +01:00
|
|
|
userinfo = string.gsub(userinfo, ":([^:]*)$",
|
|
|
|
function(p) parsed.password = p end)
|
2001-09-12 20:27:40 +02:00
|
|
|
parsed.user = userinfo
|
|
|
|
return parsed
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Rebuilds a parsed URL from its components.
|
|
|
|
-- Components are protected if any reserved or unallowed characters are found
|
|
|
|
-- Input
|
|
|
|
-- parsed: parsed URL, as returned by Public.parse
|
|
|
|
-- Returns
|
2002-12-03 00:34:41 +01:00
|
|
|
-- a stringing with the corresponding URL
|
2001-09-12 20:27:40 +02:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Public.build_url(parsed)
|
|
|
|
local url = parsed.path or ""
|
|
|
|
if parsed.params then url = url .. ";" .. parsed.params end
|
|
|
|
if parsed.query then url = url .. "?" .. parsed.query end
|
2001-09-26 22:38:47 +02:00
|
|
|
local authority = parsed.authority
|
|
|
|
if parsed.host then
|
|
|
|
authority = parsed.host
|
|
|
|
if parsed.port then authority = authority .. ":" .. parsed.port end
|
|
|
|
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
|
|
|
|
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
|
2002-12-03 00:34: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
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Public.absolute_url(base_url, relative_url)
|
2002-07-08 23:01:45 +02:00
|
|
|
local base = Public.parse_url(base_url)
|
|
|
|
local relative = Public.parse_url(relative_url)
|
2001-09-12 20:27:40 +02:00
|
|
|
if not base then return relative_url
|
|
|
|
elseif not relative then return base_url
|
|
|
|
elseif relative.scheme then return relative_url
|
|
|
|
else
|
|
|
|
relative.scheme = base.scheme
|
|
|
|
if not relative.authority then
|
|
|
|
relative.authority = base.authority
|
|
|
|
if not relative.path then
|
|
|
|
relative.path = base.path
|
|
|
|
if not relative.params then
|
|
|
|
relative.params = base.params
|
|
|
|
if not relative.query then
|
|
|
|
relative.query = base.query
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2002-07-08 23:01:45 +02:00
|
|
|
relative.path = Private.absolute_path(base.path,relative.path)
|
2001-09-12 20:27:40 +02:00
|
|
|
end
|
|
|
|
end
|
2002-07-08 23:01:45 +02:00
|
|
|
return Public.build_url(relative)
|
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
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Public.parse_path(path)
|
|
|
|
local parsed = {}
|
2001-09-26 22:38:47 +02:00
|
|
|
path = path or ""
|
2002-12-03 00:34:41 +01:00
|
|
|
path = string.gsub(path, "%s", "")
|
|
|
|
string.gsub(path, "([^/]+)", function (s) table.insert(parsed, s) end)
|
|
|
|
for i = 1, table.getn(parsed) do
|
2001-09-12 20:27:40 +02:00
|
|
|
parsed[i] = Code.unescape(parsed[i])
|
|
|
|
end
|
2002-12-03 08:20:34 +01:00
|
|
|
if string.sub(path, 1, 1) == "/" then parsed.is_absolute = 1 end
|
|
|
|
if string.sub(path, -1, -1) == "/" then parsed.is_directory = 1 end
|
2001-09-12 20:27:40 +02:00
|
|
|
return parsed
|
|
|
|
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
|
2002-12-03 00:34:41 +01:00
|
|
|
-- path: correspondin path stringing
|
2001-09-12 20:27:40 +02:00
|
|
|
-----------------------------------------------------------------------------
|
2001-09-26 22:38:47 +02:00
|
|
|
function Public.build_path(parsed, unsafe)
|
2001-09-12 20:27:40 +02:00
|
|
|
local path = ""
|
2002-12-03 00:34:41 +01:00
|
|
|
local n = table.getn(parsed)
|
2001-09-26 22:38:47 +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
|
2002-07-08 23:01:45 +02:00
|
|
|
path = path .. Private.protect_segment(parsed[i])
|
2001-09-26 22:38:47 +02:00
|
|
|
path = path .. "/"
|
|
|
|
end
|
|
|
|
if n > 0 then
|
2002-07-08 23:01:45 +02:00
|
|
|
path = path .. Private.protect_segment(parsed[n])
|
2001-09-26 22:38:47 +02:00
|
|
|
if parsed.is_directory then path = path .. "/" end
|
|
|
|
end
|
2001-09-12 20:27:40 +02:00
|
|
|
end
|
|
|
|
if parsed.is_absolute then path = "/" .. path end
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
|
2002-12-03 00:34:41 +01:00
|
|
|
function Private.make_set(t)
|
2001-09-12 20:27:40 +02:00
|
|
|
local s = {}
|
2002-12-03 00:34:41 +01:00
|
|
|
for i = 1, table.getn(t) do
|
|
|
|
s[t[i]] = 1
|
2001-09-12 20:27:40 +02:00
|
|
|
end
|
|
|
|
return s
|
|
|
|
end
|
|
|
|
|
|
|
|
-- these are allowed withing a path segment, along with alphanum
|
|
|
|
-- other characters must be escaped
|
|
|
|
Private.segment_set = Private.make_set {
|
|
|
|
"-", "_", ".", "!", "~", "*", "'", "(",
|
|
|
|
")", ":", "@", "&", "=", "+", "$", ",",
|
|
|
|
}
|
|
|
|
|
|
|
|
function Private.protect_segment(s)
|
2002-07-08 23:01:45 +02:00
|
|
|
local segment_set = Private.segment_set
|
2002-12-03 00:34:41 +01:00
|
|
|
return string.gsub(s, "(%W)", function (c)
|
2002-07-08 23:01:45 +02:00
|
|
|
if segment_set[c] then return c
|
2001-09-12 20:27:40 +02:00
|
|
|
else return Code.escape(c) end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Builds a path from a base path and a relative path
|
|
|
|
-- Input
|
|
|
|
-- base_path
|
|
|
|
-- relative_path
|
|
|
|
-- Returns
|
|
|
|
-- corresponding absolute path
|
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
function Private.absolute_path(base_path, relative_path)
|
2002-12-03 08:20:34 +01:00
|
|
|
if string.sub(relative_path, 1, 1) == "/" then return relative_path end
|
2002-12-03 00:34:41 +01:00
|
|
|
local path = string.gsub(base_path, "[^/]*$", "")
|
2001-09-12 20:27:40 +02:00
|
|
|
path = path .. relative_path
|
2002-12-03 00:34:41 +01:00
|
|
|
path = string.gsub(path, "([^/]*%./)", function (s)
|
2001-09-12 20:27:40 +02:00
|
|
|
if s ~= "./" then return s else return "" end
|
|
|
|
end)
|
2002-12-03 00:34:41 +01:00
|
|
|
path = string.gsub(path, "/%.$", "/")
|
2001-09-12 20:27:40 +02:00
|
|
|
local reduced
|
|
|
|
while reduced ~= path do
|
|
|
|
reduced = path
|
2002-12-03 00:34:41 +01:00
|
|
|
path = string.gsub(reduced, "([^/]*/%.%./)", function (s)
|
2001-09-12 20:27:40 +02:00
|
|
|
if s ~= "../../" then return "" else return s end
|
|
|
|
end)
|
|
|
|
end
|
2002-12-03 00:34:41 +01:00
|
|
|
path = string.gsub(reduced, "([^/]*/%.%.)$", function (s)
|
2001-09-12 20:27:40 +02:00
|
|
|
if s ~= "../.." then return "" else return s end
|
|
|
|
end)
|
|
|
|
return path
|
|
|
|
end
|