]> git.e2factory.org Git - e2factory.git/commitdiff
Change url module to a more encapsulated type
authorTobias Ulmer <tu@emlix.com>
Tue, 7 Aug 2012 09:54:10 +0000 (11:54 +0200)
committerTobias Ulmer <tu@emlix.com>
Wed, 8 Aug 2012 12:44:00 +0000 (14:44 +0200)
Signed-off-by: Tobias Ulmer <tu@emlix.com>
generic/url.lua

index 52aae4d95b5e6a67955af1787d2feba129336370..a2eebbe5d5bffb7d462d5ed1932864efcbb260f2 100644 (file)
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ]]
 
-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: