def do_rpc(request, async=false)
header = {
"User-Agent" => USER_AGENT,
"Content-Type" => "text/xml",
"Content-Length" => request.size.to_s,
"Connection" => (async ? "close" : "keep-alive")
}
header["Cookie"] = @cookie if @cookie
header.update(@http_header_extra) if @http_header_extra
if @auth != nil
# add authorization header
header["Authorization"] = @auth
end
resp = nil
@http_last_response = nil
if async
# use a new HTTP object for each call
Net::HTTP.version_1_2
http = Net::HTTP.new(@host, @port, @proxy_host, @proxy_port)
http.use_ssl = @use_ssl if @use_ssl
http.read_timeout = @timeout
http.open_timeout = @timeout
# post request
http.start {
resp = http.post2(@path, request, header)
}
else
# reuse the HTTP object for each call => connection alive is possible
# post request
resp = @http.post2(@path, request, header)
end
@http_last_response = resp
data = resp.body
if resp.code == "401"
# Authorization Required
raise "Authorization failed.\nHTTP-Error: #{resp.code} #{resp.message}"
elsif resp.code[0,1] != "2"
raise "HTTP-Error: #{resp.code} #{resp.message}"
end
ct = parse_content_type(resp["Content-Type"]).first
if ct != "text/xml"
if ct == "text/html"
raise "Wrong content-type: \n#{data}"
else
raise "Wrong content-type"
end
end
=begin Wordpress Workaround (hack)
expected = resp["Content-Length"] || "<unknown>"
if data.nil? or data.size == 0
raise "Wrong size. Was #{data.size}, should be #{expected}"
elsif expected.to_i != data.size and resp["Transfer-Encoding"].nil?
raise "Wrong size. Was #{data.size}, should be #{expected}"
end
=end
c = resp["Set-Cookie"]
@cookie = c if c
return data
end