From 04b4e0f9b73c83be609440f4c38702467cf46655 Mon Sep 17 00:00:00 2001 From: Tobias Ulmer Date: Tue, 7 Aug 2012 11:54:10 +0200 Subject: [PATCH] Change url module to a more encapsulated type Signed-off-by: Tobias Ulmer --- generic/url.lua | 90 ++++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/generic/url.lua b/generic/url.lua index 52aae4d..a2eebbe 100644 --- a/generic/url.lua +++ b/generic/url.lua @@ -25,52 +25,56 @@ along with this program. If not, see . ]] -module("url", package.seeall) +local url = {} --- parse -- @param url the url to parse -- @return a table holding all parsed parts of the url, or nil on error -- @return nil, or an error string on error -function parse(url) - local u = {} - --- url - -- @class table - -- @name url - -- @field url the original url as passed to the parse() function - -- @field transport the transport type - -- @field server the server part - -- @field path the path relative to the server - -- @field servername the server name from the server part - -- @field user the user name from the server part (optional) - -- @field pass the password from the server part (optional) - -- @field port given server port (optional) - if not url then - return nil, "missing parameter: url" - end - u.url = url - -- parse: transport://server/path - u.transport, u.server, u.path = - u.url:match("(%S+)://([^/]*)(.*)") - if not u.transport then - return nil, string.format("can't parse url: %s", url) - end - -- remove leading slashes from the path - u.path = u.path:match("^[/]*(.*)") - -- parse the server part - if u.server:match("(%S+):(%S+)@(%S+)") then - -- user:pass@host - u.user, u.pass, u.servername = - u.server:match("(%S+):(%S+)@(%S+)") - elseif u.server:match("(%S+)@(%S+)") then - -- user@host - u.user, u.servername = u.server:match("(%S+)@(%S+)") - else - u.servername = u.server - end - if u.server:match(":(%d+)$") then - u.port = u.server:match(":(%d+)$") - u.server = string.gsub(u.server, ":%d+$","") -- Remove port from server string. - u.servername = string.gsub(u.servername, ":%d+$","") -- Remove port from server string. - end - return u, nil +function url.parse(url) + local u = {} + --- url + -- @class table + -- @name url + -- @field url the original url as passed to the parse() function + -- @field transport the transport type + -- @field server the server part + -- @field path the path relative to the server + -- @field servername the server name from the server part + -- @field user the user name from the server part (optional) + -- @field pass the password from the server part (optional) + -- @field port given server port (optional) + if not url then + return nil, "missing parameter: url" + end + u.url = url + -- parse: transport://server/path + u.transport, u.server, u.path = + u.url:match("(%S+)://([^/]*)(.*)") + if not u.transport then + return nil, string.format("can't parse url: %s", url) + end + -- remove leading slashes from the path + u.path = u.path:match("^[/]*(.*)") + -- parse the server part + if u.server:match("(%S+):(%S+)@(%S+)") then + -- user:pass@host + u.user, u.pass, u.servername = + u.server:match("(%S+):(%S+)@(%S+)") + elseif u.server:match("(%S+)@(%S+)") then + -- user@host + u.user, u.servername = u.server:match("(%S+)@(%S+)") + else + u.servername = u.server + end + if u.server:match(":(%d+)$") then + u.port = u.server:match(":(%d+)$") + u.server = string.gsub(u.server, ":%d+$","") -- Remove port from server string. + u.servername = string.gsub(u.servername, ":%d+$","") -- Remove port from server string. + end + return u, nil end + +return url + +-- vim:sw=4:sts=4:et: -- 2.39.5