6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/osdb/xmlrpc_monkey_patch.rb', line 6
def do_rpc(request, async=false)
= {
"User-Agent" => USER_AGENT,
"Content-Type" => "text/xml; charset=utf-8",
"Content-Length" => request.bytesize.to_s,
"Connection" => (async ? "close" : "keep-alive")
}
["Cookie"] = @cookie if @cookie
.update() if
if @auth != nil
["Authorization"] = @auth
end
resp = nil
@http_last_response = nil
if async
http = net_http(@host, @port, @proxy_host, @proxy_port)
http.use_ssl = @use_ssl if @use_ssl
http.read_timeout = @timeout
http.open_timeout = @timeout
http.start {
resp = http.request_post(@path, request, )
}
else
@http.start if not @http.started?
resp = @http.request_post(@path, request, )
end
@http_last_response = resp
data = resp.body
if resp.code == "401"
raise "Authorization failed.\nHTTP-Error: #{resp.code} #{resp.message}"
elsif resp.code[0,1] != "2"
raise "HTTP-Error: #{resp.code} #{resp.message}"
end
ct_expected = resp["Content-Type"] || 'text/xml'
ct = parse_content_type(ct_expected).first
if ct != "text/xml"
if ct == "text/html"
raise "Wrong content-type (received '#{ct}' but expected 'text/xml'): \n#{data}"
else
raise "Wrong content-type (received '#{ct}' but expected 'text/xml')"
end
end
expected = resp["Content-Length"] || "<unknown>"
if data.nil? or data.bytesize == 0
raise "Wrong size. Was #{data.bytesize}, should be #{expected}"
elsif expected != "<unknown>" and expected.to_i != data.bytesize and resp["Transfer-Encoding"].nil?
end
set_cookies = resp.get_fields("Set-Cookie")
if set_cookies and !set_cookies.empty?
require 'webrick/cookie'
@cookie = set_cookies.collect do |set_cookie|
cookie = WEBrick::Cookie.parse_set_cookie(set_cookie)
WEBrick::Cookie.new(cookie.name, cookie.value).to_s
end.join("; ")
end
return data
end
|