Class: XMLRPC::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/em-xmlrpc-client.rb

Instance Method Summary collapse

Constructor Details

#initialize(host = nil, path = nil, port = nil, proxy_host = nil, proxy_port = nil, user = nil, password = nil, use_ssl = nil, timeout = nil) ⇒ Client

Returns a new instance of Client.



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
# File 'lib/em-xmlrpc-client.rb', line 7

def initialize(host=nil, path=nil, port=nil, proxy_host=nil, proxy_port=nil,
               user=nil, password=nil, use_ssl=nil, timeout=nil)

  @http_header_extra = nil
  @http_last_response = nil
  @cookie = nil

  @host       = host || "localhost"
  @path       = path || "/RPC2"
  @proxy_host = proxy_host
  @proxy_port = proxy_port
  @proxy_host ||= 'localhost' if @proxy_port != nil
  @proxy_port ||= 8080 if @proxy_host != nil
  @use_ssl    = use_ssl || false
  @timeout    = timeout || 30

  if use_ssl
    require "net/https"
    @port = port || 443
  else
    @port = port || 80
  end

  @user, @password = user, password

  set_auth

  # convert ports to integers
  @port = @port.to_i if @port != nil
  @proxy_port = @proxy_port.to_i if @proxy_port != nil

  if defined?(EM) and EM.reactor_running?
    require "em-http"
    require "ostruct"
    require "fiber"
  else
    # HTTP object for synchronous calls
    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
  end

  @parser = nil
  @create = nil
end

Instance Method Details

#do_rpc(request, async = false) ⇒ Object



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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/em-xmlrpc-client.rb', line 55

def do_rpc(request, async=false)
  header = {
   "User-Agent"     =>  USER_AGENT,
   "Content-Type"   => "text/xml; charset=utf-8",
   "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

  @http_last_response = nil

  if defined?(EM) and EM.reactor_running?
    resp = do_rpc_em_http(async, request, header)
  else
    resp = do_rpc_net_http(async, 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 (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.size == 0
    raise "Wrong size. Was #{data.size}, should be #{expected}"
  elsif expected != "<unknown>" and expected.to_i != data.size and resp["Transfer-Encoding"].nil?
    raise "Wrong size. Was #{data.size}, should be #{expected}"
  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

#do_rpc_em_http(async, request, header) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/em-xmlrpc-client.rb', line 126

def do_rpc_em_http(async, request, header)
  fiber = Fiber.current
  http = EM::HttpRequest.new("http://#{@host}:#{@port}#{@path}").post :body => request, :timeout => @timeout
  http.callback{ fiber.resume }
  http.errback do
    # Unfortunately, we can't determine exactly what the error is using EventMachine < 1.0.
    error = RuntimeError.new("connection or timeout error")
    fiber.resume(error)
  end

  e = Fiber.yield and raise(e)

  # Ducktype our response object.
  resp = OpenStruct.new :code     => http.response_header.http_status.to_s,
                        :message  => http.response_header.http_reason,
                        :header   => http.response_header.to_hash,
                        :body     => http.response.to_s

  resp.header["Content-Type"]      = resp.header["CONTENT_TYPE"]
  resp.header["Content-Length"]    = resp.header["CONTENT_LENGTH"]
  resp.header["Transfer-Encoding"] = resp.header["TRANSFER_ENCODING"]
  resp.header["Set-Cookie"]        = resp.header["SET_COOKIE"]

  def resp.[](name)
    header[name]
  end
  def resp.get_fields(name)
    value = header[name]
    value and [value].flatten
  end
  
  resp
end

#do_rpc_net_http(async, request, header) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/em-xmlrpc-client.rb', line 160

def do_rpc_net_http(async, request, header)
  resp = 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
    # we must start connection explicitely first time so that http.request
    # does not assume that we don't want keepalive
    @http.start if not @http.started?

    # post request
    resp = @http.post2(@path, request, header)
  end
  
  resp
end

#timeout=(new_timeout) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/em-xmlrpc-client.rb', line 118

def timeout=(new_timeout)
  @timeout = new_timeout
  unless defined?(EM) and EM.reactor_running?
    @http.read_timeout = @timeout
    @http.open_timeout = @timeout
  end
end