httpclient: Add basic auth support
authorSteven Barth <steven@midlink.org>
Tue, 8 Dec 2009 12:13:26 +0000 (12:13 +0000)
committerSteven Barth <steven@midlink.org>
Tue, 8 Dec 2009 12:13:26 +0000 (12:13 +0000)
libs/httpclient/luasrc/httpclient.lua

index 69abddff327ea5a92f82a8386e3598a713bc511a..1f0e55b3185b60e077484e066795393e22704305 100644 (file)
@@ -107,7 +107,14 @@ end
 --
 function request_raw(uri, options)
        options = options or {}
-       local pr, host, port, path = uri:match("(%w+)://([%w-.]+):?([0-9]*)(.*)")
+       local pr, auth, host, port, path
+       if uri:find("@") then
+               pr, auth, host, port, path =
+                       uri:match("(%w+)://(.+)@([%w-.]+):?([0-9]*)(.*)")
+       else
+               pr, host, port, path = uri:match("(%w+)://([%w-.]+):?([0-9]*)(.*)")
+       end
+
        if not host then
                return nil, -1, "unable to parse URI"
        end
@@ -128,6 +135,10 @@ function request_raw(uri, options)
                headers.Connection = "close"
        end
        
+       if auth and not headers.Authorization then
+               headers.Authorization = "Basic " .. nixio.bin.b64encode(auth)
+       end
+
        local sock, code, msg = nixio.connect(host, port)
        if not sock then
                return nil, code, msg