mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-26 12:28:21 +01:00
Changed build_url to compose from smaller parts
Changed build_path to add an option not to protect path components
This commit is contained in:
parent
6f02d1444b
commit
ef5322a92a
44
src/url.lua
44
src/url.lua
@ -70,7 +70,20 @@ function Public.build_url(parsed)
|
|||||||
local url = parsed.path or ""
|
local url = parsed.path or ""
|
||||||
if parsed.params then url = url .. ";" .. parsed.params end
|
if parsed.params then url = url .. ";" .. parsed.params end
|
||||||
if parsed.query then url = url .. "?" .. parsed.query end
|
if parsed.query then url = url .. "?" .. parsed.query end
|
||||||
if parsed.authority then url = "//" .. parsed.authority .. url end
|
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
|
||||||
if parsed.scheme then url = parsed.scheme .. ":" .. url end
|
if parsed.scheme then url = parsed.scheme .. ":" .. url end
|
||||||
if parsed.fragment then url = url .. "#" .. parsed.fragment end
|
if parsed.fragment then url = url .. "#" .. parsed.fragment end
|
||||||
return gsub(url, "%s", "")
|
return gsub(url, "%s", "")
|
||||||
@ -119,6 +132,7 @@ end
|
|||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Public.parse_path(path)
|
function Public.parse_path(path)
|
||||||
local parsed = {}
|
local parsed = {}
|
||||||
|
path = path or ""
|
||||||
path = gsub(path, "%s", "")
|
path = gsub(path, "%s", "")
|
||||||
gsub(path, "([^/]+)", function (s) tinsert(%parsed, s) end)
|
gsub(path, "([^/]+)", function (s) tinsert(%parsed, s) end)
|
||||||
for i = 1, getn(parsed) do
|
for i = 1, getn(parsed) do
|
||||||
@ -133,19 +147,31 @@ end
|
|||||||
-- Builds a path component from its segments, escaping protected characters.
|
-- Builds a path component from its segments, escaping protected characters.
|
||||||
-- Input
|
-- Input
|
||||||
-- parsed: path segments
|
-- parsed: path segments
|
||||||
|
-- unsafe: if true, segments are not protected before path is built
|
||||||
-- Returns
|
-- Returns
|
||||||
-- path: correspondin path string
|
-- path: correspondin path string
|
||||||
-----------------------------------------------------------------------------
|
-----------------------------------------------------------------------------
|
||||||
function Public.build_path(parsed)
|
function Public.build_path(parsed, unsafe)
|
||||||
local path = ""
|
local path = ""
|
||||||
local n = getn(parsed)
|
local n = getn(parsed)
|
||||||
for i = 1, n-1 do
|
if unsafe then
|
||||||
path = path .. %Private.protect_segment(parsed[i])
|
for i = 1, n-1 do
|
||||||
path = path .. "/"
|
path = path .. parsed[i]
|
||||||
end
|
path = path .. "/"
|
||||||
if n > 0 then
|
end
|
||||||
path = path .. %Private.protect_segment(parsed[n])
|
if n > 0 then
|
||||||
if parsed.is_directory then path = path .. "/" end
|
path = path .. parsed[n]
|
||||||
|
if parsed.is_directory then path = path .. "/" end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
for i = 1, n-1 do
|
||||||
|
path = path .. %Private.protect_segment(parsed[i])
|
||||||
|
path = path .. "/"
|
||||||
|
end
|
||||||
|
if n > 0 then
|
||||||
|
path = path .. %Private.protect_segment(parsed[n])
|
||||||
|
if parsed.is_directory then path = path .. "/" end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if parsed.is_absolute then path = "/" .. path end
|
if parsed.is_absolute then path = "/" .. path end
|
||||||
return path
|
return path
|
||||||
|
Loading…
Reference in New Issue
Block a user