# signature { license=0, seed=457995617, key="74ecaad013f6185927dccc55a4c28048" }
local socket = require "socket"
local format,find,match = string.format, string.find, string.match

local _M = require "xmlpro.core"
_M._VERSION   = "XmlPRO 0.1.0"
_M.authorization = "dGVjaDoxMjM0NTY3OA=="

_M.login = function( name, password )
  local b64 = require("mime").b64
  _M.authorization = b64(name..":"..password)
end
 
_M.post = function( text )
  local request = format("POST /dbms.xml HTTP/1.1\r\nAuthorization: Basic %s\r\nContent-Length: %d\r\n\r\n%s",
      _M.authorization, #text, text)
  local s = socket.connect("127.0.0.1", _M.port)
  s:send( request )
  
  local response
  local status = s:receive():match(" (%d%d%d)")
  if "200" == status then
    local len=0
    repeat 
      local line = s:receive()  -- receive header lines
      len = line:lower():match( "^content%-length:%s*(%d+)") or len
    until line==""
    
    response = s:receive( tonumber(len) )
  end
  s:close()
  return response, status
end

return _M
