diff --git a/src/http.lua b/src/http.lua index cae7d8f..c5b6735 100644 --- a/src/http.lua +++ b/src/http.lua @@ -4,6 +4,7 @@ --------------------------------------------------------------------- local error, tonumber, tostring, unpack = error, tonumber, tostring, unpack +local pairs = pairs local ltn12 = require"ltn12" local request = require"socket.http".request @@ -20,20 +21,37 @@ module("xmlrpc.http") -- @return Table with the response (could be a `fault' or a `params' -- XML-RPC element). --------------------------------------------------------------------- -function call (url, method, ...) +function call (cookies, url, method, ...) local request_sink, tbody = ltn12.sink.table() local request_body = xmlrpc.clEncode(method, ...) + local reqheaders = { ["User-agent"] = xmlrpc._PKGNAME .. " " .. xmlrpc._VERSION, + ["Content-type"] = "text/xml", + ["content-length"] = tostring (string.len (request_body)), + }; + + cooks = "" + for key,value in pairs(cookies) do + if cooks ~= "" then cooks = cooks .."," end + cooks = cooks .. key .. "=" .. value .. "; " + end + if cooks ~= "" then reqheaders["Cookie"] = cooks end + local err, code, headers, status = request { url = url, method = "POST", source = ltn12.source.string (request_body), sink = request_sink, - headers = { - ["User-agent"] = xmlrpc._PKGNAME .. " " .. xmlrpc._VERSION, - ["Content-type"] = "text/xml", - ["content-length"] = tostring (string.len (request_body)), - }, + headers = reqheaders, } + + if headers["set-cookie"] ~= nil then + for s in string.gmatch(headers["set-cookie"], "[^,]+[,$]") do + for k, v in string.gmatch(s, "([^=]+)=([^;]+);.+") do + cookies[string.gsub(k, "^%s*(.-)%s*$", "%1")] = v + end + end + end + local body = table.concat (tbody) if tonumber (code) == 200 then return xmlrpc.clDecode (body)