1 parent
ae09757e48
commit
4d38174e30
4 files changed
+14
-20
No files matched your search
@@ -74,7 +74,7 @@ function WebDav:uploadFile(url, address, username, password, local_path, callbac
|
||||
end
|
||||
|
||||
function WebDav:createFolder(url, address, username, password, folder_name, callback_close)
|
||||
local code_response = WebDavApi:createFolder(address .. WebDavApi.urlEncode(url .. "/" .. folder_name), username, password, folder_name)
|
||||
local code_response = WebDavApi:createFolder(address .. util.urlEncode(url .. "/" .. folder_name, "/"), username, password, folder_name)
|
||||
if code_response == 201 then
|
||||
if callback_close then
|
||||
callback_close()
|
||||
|
||||
@@ -27,21 +27,9 @@ function WebDavApi.rtrim_slashes(s)
|
||||
return s:sub(1, n)
|
||||
end
|
||||
|
||||
-- Variant of util.urlEncode that doesn't encode the /
|
||||
function WebDavApi.urlEncode(url_data)
|
||||
local char_to_hex = function(c)
|
||||
return string.format("%%%02X", string.byte(c))
|
||||
end
|
||||
if url_data == nil then
|
||||
return
|
||||
end
|
||||
url_data = url_data:gsub("([^%w%/%-%.%_%~%!%*%'%(%)])", char_to_hex)
|
||||
return url_data
|
||||
end
|
||||
|
||||
-- Append path to address with a slash separator, trimming any unwanted slashes in the process.
|
||||
function WebDavApi:getJoinedPath(address, path)
|
||||
local path_encoded = self.urlEncode(path) or ""
|
||||
local path_encoded = util.urlEncode(path, "/") or ""
|
||||
-- Strip leading & trailing slashes from `path`
|
||||
local sane_path = self.trim_slashes(path_encoded)
|
||||
-- Strip trailing slashes from `address` for now
|
||||
@@ -61,7 +49,7 @@ function WebDavApi:listFolder(address, user, pass, folder_path, folder_mode)
|
||||
address = self.rtrim_slashes(address)
|
||||
-- Join our final URL, which *must* have a trailing / (it's a URL)
|
||||
-- This is where we deviate from getJoinedPath ;).
|
||||
local webdav_url = address .. "/" .. self.urlEncode(path)
|
||||
local webdav_url = address .. "/" .. util.urlEncode(path, "/")
|
||||
if webdav_url:sub(-1) ~= "/" then
|
||||
webdav_url = webdav_url .. "/"
|
||||
end
|
||||
|
||||
+9
-3
@@ -1452,17 +1452,23 @@ end
|
||||
|
||||
--- Encode URL also known as percent-encoding see https://en.wikipedia.org/wiki/Percent-encoding
|
||||
--- @string text the string to encode
|
||||
--- @string preserve_chars a string containing all the charactes to preserve unencoded - e.g. "/"
|
||||
--- @treturn encode string
|
||||
--- Taken from https://gist.github.com/liukun/f9ce7d6d14fa45fe9b924a3eed5c3d99
|
||||
function util.urlEncode(url)
|
||||
--- Originally taken from https://gist.github.com/liukun/f9ce7d6d14fa45fe9b924a3eed5c3d99
|
||||
--- Modified to comply with RFC 3986's unreserved characters definition:
|
||||
--- ALPHA (A-Z, a-z), DIGIT (0-9), '-', '.', '_', '~'
|
||||
function util.urlEncode(url, preserve_chars)
|
||||
local char_to_hex = function(c)
|
||||
return string.format("%%%02X", string.byte(c))
|
||||
end
|
||||
preserve_chars = preserve_chars or ""
|
||||
local pattern_base = "^%w%-%._~"
|
||||
local pattern = string.format("([%s%s])", pattern_base, preserve_chars)
|
||||
if url == nil then
|
||||
return
|
||||
end
|
||||
url = url:gsub("\n", "\r\n")
|
||||
url = url:gsub("([^%w%-%.%_%~%!%*%'%(%)])", char_to_hex)
|
||||
url = url:gsub(pattern, char_to_hex)
|
||||
return url
|
||||
end
|
||||
|
||||
|
||||
@@ -474,8 +474,8 @@ describe("util module", function()
|
||||
it("should encode string", function()
|
||||
assert.is_equal("Secret_Password123", util.urlEncode("Secret_Password123"))
|
||||
assert.is_equal("Secret%20Password123", util.urlEncode("Secret Password123"))
|
||||
assert.is_equal("S*cret%3DP%40%24%24word*!%23%3F", util.urlEncode("S*cret=P@$$word*!#?"))
|
||||
assert.is_equal("~%5E-_%5C%25!*'()%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D",
|
||||
assert.is_equal("S%2Acret%3DP%40%24%24word%2A%21%23%3F", util.urlEncode("S*cret=P@$$word*!#?"))
|
||||
assert.is_equal("~%5E-_%5C%25%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D",
|
||||
util.urlEncode("~^-_\\%!*'();:@&=+$,/?#[]"))
|
||||
end)
|
||||
it("should decode string", function()
|
||||
|
||||
Reference in new issue
Block a user