luci-base: fold luci.http.protocol into luci.http
[project/luci.git] / libs / luci-lib-httpclient / luasrc / httpclient.lua
index e9fec5d..04175b3 100644 (file)
@@ -1,16 +1,5 @@
---[[
-LuCI - Lua Development Framework
-
-Copyright 2009 Steven Barth <steven@midlink.org>
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-$Id$
-]]--
+-- Copyright 2009 Steven Barth <steven@midlink.org>
+-- Licensed to the public under the Apache License 2.0.
 
 require "nixio.util"
 local nixio = require "nixio"
@@ -18,7 +7,7 @@ local nixio = require "nixio"
 local ltn12 = require "luci.ltn12"
 local util = require "luci.util"
 local table = require "table"
-local http = require "luci.http.protocol"
+local http = require "luci.http"
 local date = require "luci.http.protocol.date"
 
 local type, pairs, ipairs, tonumber = type, pairs, ipairs, tonumber
@@ -108,7 +97,11 @@ end
 function request_raw(uri, options)
        options = options or {}
        local pr, auth, host, port, path
-       
+
+       if options.params then
+               uri = uri .. '?' .. http.urlencode_params(options.params)
+       end
+
        if uri:find("%[") then
                if uri:find("@") then
                        pr, auth, host, port, path = uri:match("(%w+)://(.+)@(%b[]):?([0-9]*)(.*)")
@@ -187,20 +180,8 @@ function request_raw(uri, options)
                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" or type(v) == "number" then
-                       message[#message+1] = k .. ": " .. v
-               elseif type(v) == "table" then
-                       for i, j in ipairs(v) do
-                               message[#message+1] = k .. ": " .. j
-                       end
-               end
-       end
-       
        if options.cookies then
+               local cookiedata = {}
                for _, c in ipairs(options.cookies) do
                        local cdo = c.flags.domain
                        local cpa = c.flags.path
@@ -208,11 +189,29 @@ function request_raw(uri, options)
                         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
+                               cookiedata[#cookiedata+1] = c.key .. "=" .. c.value
                        end 
                end
+               if headers["Cookie"] then
+                       headers["Cookie"] = headers["Cookie"] .. "; " .. table.concat(cookiedata, "; ")
+               else
+                       headers["Cookie"] = table.concat(cookiedata, "; ")
+               end
        end
+
+       -- Assemble message
+       local message = {(options.method or "GET") .. " " .. path .. " " .. protocol}
        
+       for k, v in pairs(headers) do
+               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
+                               message[#message+1] = k .. ": " .. j
+                       end
+               end
+       end
+
        message[#message+1] = ""
        message[#message+1] = ""
        
@@ -334,7 +333,7 @@ function request_raw(uri, options)
                end
        end
        
-       return response.code, response, linesrc(true), sock
+       return response.code, response, linesrc(true)..sock:readall(), sock
 end
 
 function cookie_parse(cookiestr)