luci-0.11: merge r9399-r9402 and r9412
[project/luci.git] / libs / httpclient / luasrc / httpclient.lua
index 01ef970d174a21ace0a6e0e2d5d03ea31ea0c0cf..93eed05c63d4f4c723fdca58b8740a43590efca0 100644 (file)
@@ -22,6 +22,7 @@ local http = require "luci.http.protocol"
 local date = require "luci.http.protocol.date"
 
 local type, pairs, ipairs, tonumber = type, pairs, ipairs, tonumber
+local unpack = unpack
 
 module "luci.httpclient"
 
@@ -91,7 +92,7 @@ function request_to_source(uri, options)
        if not status then
                return status, response, buffer
        elseif status ~= 200 and status ~= 206 then
-               return nil, status, response
+               return nil, status, buffer
        end
        
        if response.headers["Transfer-Encoding"] == "chunked" then
@@ -106,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
@@ -127,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
@@ -160,11 +172,15 @@ function request_raw(uri, options)
                options.method = options.method or "POST"
        end
        
+       if type(options.body) == "function" then
+               options.method = options.method or "POST"
+       end
+
        -- Assemble message
        local message = {(options.method or "GET") .. " " .. path .. " " .. protocol}
        
        for k, v in pairs(headers) do
-               if type(v) == "string" then
+               if type(v) == "string" or type(v) == "number" then
                        message[#message+1] = k .. ": " .. v
                elseif type(v) == "table" then
                        for i, j in ipairs(v) do
@@ -178,7 +194,7 @@ function request_raw(uri, options)
                        local cdo = c.flags.domain
                        local cpa = c.flags.path
                        if   (cdo == host or cdo == "."..host or host:sub(-#cdo) == cdo) 
-                        and (cpa == "/" or cpa .. "/" == path:sub(#cpa+1))
+                        and (cpa == path or cpa == "/" or cpa .. "/" == path:sub(#cpa+1))
                         and (not c.flags.secure or pr == "https")
                        then
                                message[#message+1] = "Cookie: " .. c.key .. "=" .. c.value
@@ -194,6 +210,12 @@ function request_raw(uri, options)
        
        if type(options.body) == "string" then
                sock:sendall(options.body)
+       elseif type(options.body) == "function" then
+               local res = {options.body(sock)}
+               if not res[1] then
+                       sock:close()
+                       return unpack(res)
+               end
        end
        
        -- Create source and fetch response
@@ -201,24 +223,28 @@ function request_raw(uri, options)
        local line, code, error = linesrc()
        
        if not line then
+               sock:close()
                return nil, code, error
        end
        
-       local protocol, status, msg = line:match("^(HTTP/[0-9.]+) ([0-9]+) (.*)")
+       local protocol, status, msg = line:match("^([%w./]+) ([0-9]+) (.*)")
        
        if not protocol then
+               sock:close()
                return nil, -3, "invalid response magic: " .. line
        end
        
-       local response = {status = line, headers = {}, code = 0, cookies = {}}
+       local response = {
+               status = line, headers = {}, code = 0, cookies = {}, uri = uri
+       }
        
        line = linesrc()
        while line and line ~= "" do
                local key, val = line:match("^([%w-]+)%s?:%s?(.*)")
                if key and key ~= "Status" then
-                       if type(response[key]) == "string" then
+                       if type(response.headers[key]) == "string" then
                                response.headers[key] = {response.headers[key], val}
-                       elseif type(response[key]) == "table" then
+                       elseif type(response.headers[key]) == "table" then
                                response.headers[key][#response.headers[key]+1] = val
                        else
                                response.headers[key] = val
@@ -228,6 +254,7 @@ function request_raw(uri, options)
        end
        
        if not line then
+               sock:close()
                return nil, -4, "protocol error"
        end
        
@@ -278,15 +305,21 @@ function request_raw(uri, options)
        if response.code and options.depth > 0 then
                if response.code == 301 or response.code == 302 or response.code == 307
                 and response.headers.Location then
-                       local nexturi = response.headers.Location
-                       if not nexturi:find("https?://") then
-                               nexturi = pr .. "://" .. host .. ":" .. port .. nexturi
+                       local nuri = response.headers.Location or response.headers.location
+                       if not nuri then
+                               return nil, -5, "invalid reference"
+                       end
+                       if not nuri:find("https?://") then
+                               nuri = pr .. "://" .. host .. ":" .. port .. nuri
                        end
                        
                        options.depth = options.depth - 1
+                       if options.headers then
+                               options.headers.Host = nil
+                       end
                        sock:close()
                        
-                       return request_raw(nexturi, options)
+                       return request_raw(nuri, options)
                end
        end
        
@@ -322,4 +355,4 @@ function cookie_create(cookie)
        end
 
        return table.concat(cookiedata, "; ")
-end
\ No newline at end of file
+end