2012-09-12 22:22:10 -04:00
|
|
|
# xmlrpc/client.rb
|
|
|
|
# Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
|
|
|
|
#
|
|
|
|
# Released under the same term of license as Ruby.
|
|
|
|
#
|
|
|
|
# History
|
|
|
|
# $Id$
|
|
|
|
#
|
2003-07-19 06:05:54 -04:00
|
|
|
require "xmlrpc/parser"
|
|
|
|
require "xmlrpc/create"
|
|
|
|
require "xmlrpc/config"
|
|
|
|
require "xmlrpc/utils" # ParserWriterChooseMixin
|
|
|
|
require "net/http"
|
2012-03-02 18:12:32 -05:00
|
|
|
require "uri"
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
module XMLRPC # :nodoc:
|
|
|
|
|
|
|
|
# Provides remote procedure calls to a XML-RPC server.
|
|
|
|
#
|
|
|
|
# After setting the connection-parameters with XMLRPC::Client.new which
|
|
|
|
# creates a new XMLRPC::Client instance, you can execute a remote procedure
|
|
|
|
# by sending the XMLRPC::Client#call or XMLRPC::Client#call2
|
|
|
|
# message to this new instance.
|
|
|
|
#
|
|
|
|
# The given parameters indicate which method to call on the remote-side and
|
|
|
|
# of course the parameters for the remote procedure.
|
|
|
|
#
|
|
|
|
# require "xmlrpc/client"
|
|
|
|
#
|
|
|
|
# server = XMLRPC::Client.new("www.ruby-lang.org", "/RPC2", 80)
|
|
|
|
# begin
|
|
|
|
# param = server.call("michael.add", 4, 5)
|
|
|
|
# puts "4 + 5 = #{param}"
|
|
|
|
# rescue XMLRPC::FaultException => e
|
|
|
|
# puts "Error:"
|
|
|
|
# puts e.faultCode
|
|
|
|
# puts e.faultString
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# or
|
|
|
|
#
|
|
|
|
# require "xmlrpc/client"
|
|
|
|
#
|
|
|
|
# server = XMLRPC::Client.new("www.ruby-lang.org", "/RPC2", 80)
|
|
|
|
# ok, param = server.call2("michael.add", 4, 5)
|
|
|
|
# if ok then
|
|
|
|
# puts "4 + 5 = #{param}"
|
|
|
|
# else
|
|
|
|
# puts "Error:"
|
|
|
|
# puts param.faultCode
|
|
|
|
# puts param.faultString
|
|
|
|
# end
|
2003-07-19 06:05:54 -04:00
|
|
|
class Client
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
USER_AGENT = "XMLRPC::Client (Ruby #{RUBY_VERSION})"
|
|
|
|
|
|
|
|
include ParserWriterChooseMixin
|
|
|
|
include ParseContentType
|
|
|
|
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Creates an object which represents the remote XML-RPC server on the
|
|
|
|
# given +host+. If the server is CGI-based, +path+ is the
|
|
|
|
# path to the CGI-script, which will be called, otherwise (in the
|
|
|
|
# case of a standalone server) +path+ should be <tt>"/RPC2"</tt>.
|
|
|
|
# +port+ is the port on which the XML-RPC server listens.
|
|
|
|
#
|
|
|
|
# If +proxy_host+ is given, then a proxy server listening at
|
|
|
|
# +proxy_host+ is used. +proxy_port+ is the port of the
|
|
|
|
# proxy server.
|
|
|
|
#
|
|
|
|
# Default values for +host+, +path+ and +port+ are 'localhost', '/RPC2' and
|
|
|
|
# '80' respectively using SSL '443'.
|
|
|
|
#
|
|
|
|
# If +user+ and +password+ are given, each time a request is sent,
|
|
|
|
# an Authorization header is sent. Currently only Basic Authentication is
|
|
|
|
# implemented, no Digest.
|
|
|
|
#
|
|
|
|
# If +use_ssl+ is set to +true+, communication over SSL is enabled.
|
|
|
|
#
|
|
|
|
# Parameter +timeout+ is the time to wait for a XML-RPC response, defaults to 30.
|
2009-03-05 22:56:38 -05:00
|
|
|
def initialize(host=nil, path=nil, port=nil, proxy_host=nil, proxy_port=nil,
|
2003-07-19 06:05:54 -04:00
|
|
|
user=nil, password=nil, use_ssl=nil, timeout=nil)
|
|
|
|
|
2003-11-17 16:30:31 -05:00
|
|
|
@http_header_extra = nil
|
2009-03-05 22:56:38 -05:00
|
|
|
@http_last_response = nil
|
2003-11-17 16:30:31 -05:00
|
|
|
@cookie = nil
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
@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
|
|
|
|
|
|
|
|
# HTTP object for synchronous calls
|
2012-03-05 17:19:35 -05:00
|
|
|
@http = net_http(@host, @port, @proxy_host, @proxy_port)
|
2003-07-19 06:05:54 -04:00
|
|
|
@http.use_ssl = @use_ssl if @use_ssl
|
|
|
|
@http.read_timeout = @timeout
|
|
|
|
@http.open_timeout = @timeout
|
|
|
|
|
|
|
|
@parser = nil
|
|
|
|
@create = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2005-06-19 11:47:31 -04:00
|
|
|
class << self
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Creates an object which represents the remote XML-RPC server at the
|
|
|
|
# given +uri+. The URI should have a host, port, path, user and password.
|
|
|
|
# Example: https://user:password@host:port/path
|
|
|
|
#
|
|
|
|
# Raises an ArgumentError if the +uri+ is invalid,
|
|
|
|
# or if the protocol isn't http or https.
|
|
|
|
#
|
|
|
|
# If a +proxy+ is given it should be in the form of "host:port".
|
|
|
|
#
|
|
|
|
# The optional +timeout+ defaults to 30 seconds.
|
2005-06-19 11:47:31 -04:00
|
|
|
def new2(uri, proxy=nil, timeout=nil)
|
2012-03-02 18:46:17 -05:00
|
|
|
begin
|
|
|
|
url = URI(uri)
|
|
|
|
rescue URI::InvalidURIError => e
|
|
|
|
raise ArgumentError, e.message, e.backtrace
|
|
|
|
end
|
|
|
|
|
|
|
|
unless URI::HTTP === url
|
|
|
|
raise ArgumentError, "Wrong protocol specified. Only http or https allowed!"
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2012-03-02 18:46:17 -05:00
|
|
|
proto = url.scheme
|
|
|
|
user = url.user
|
|
|
|
passwd = url.password
|
|
|
|
host = url.host
|
|
|
|
port = url.port
|
|
|
|
path = url.path.empty? ? nil : url.request_uri
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
proxy_host, proxy_port = (proxy || "").split(":")
|
2012-03-02 18:12:32 -05:00
|
|
|
proxy_port = proxy_port.to_i if proxy_port
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
self.new(host, path, port, proxy_host, proxy_port, user, passwd, (proto == "https"), timeout)
|
|
|
|
end
|
|
|
|
|
2005-06-19 11:47:31 -04:00
|
|
|
alias new_from_uri new2
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Receives a Hash and calls XMLRPC::Client.new
|
|
|
|
# with the corresponding values.
|
|
|
|
#
|
|
|
|
# The +hash+ parameter has following case-insensitive keys:
|
|
|
|
# * host
|
|
|
|
# * path
|
|
|
|
# * port
|
|
|
|
# * proxy_host
|
|
|
|
# * proxy_port
|
|
|
|
# * user
|
|
|
|
# * password
|
|
|
|
# * use_ssl
|
|
|
|
# * timeout
|
2005-06-19 11:47:31 -04:00
|
|
|
def new3(hash={})
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
# convert all keys into lowercase strings
|
|
|
|
h = {}
|
|
|
|
hash.each { |k,v| h[k.to_s.downcase] = v }
|
|
|
|
|
|
|
|
self.new(h['host'], h['path'], h['port'], h['proxy_host'], h['proxy_port'], h['user'], h['password'],
|
|
|
|
h['use_ssl'], h['timeout'])
|
|
|
|
end
|
|
|
|
|
2005-06-19 11:47:31 -04:00
|
|
|
alias new_from_hash new3
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2013-06-13 10:07:28 -04:00
|
|
|
# Returns the Net::HTTP object for the client. If you want to
|
|
|
|
# change HTTP client options except header, cookie, timeout,
|
|
|
|
# user and password, use Net::HTTP directly.
|
|
|
|
#
|
|
|
|
# Since 2.1.0.
|
|
|
|
attr_reader :http
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Add additional HTTP headers to the request
|
2003-11-17 16:30:31 -05:00
|
|
|
attr_accessor :http_header_extra
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Returns the Net::HTTPResponse object of the last RPC.
|
2003-11-17 16:30:31 -05:00
|
|
|
attr_reader :http_last_response
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Get and set the HTTP Cookie header.
|
2003-11-17 16:30:31 -05:00
|
|
|
attr_accessor :cookie
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-11-17 16:30:31 -05:00
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Return the corresponding attributes.
|
2003-07-19 06:05:54 -04:00
|
|
|
attr_reader :timeout, :user, :password
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Sets the Net::HTTP#read_timeout and Net::HTTP#open_timeout to
|
|
|
|
# +new_timeout+
|
2003-07-19 06:05:54 -04:00
|
|
|
def timeout=(new_timeout)
|
|
|
|
@timeout = new_timeout
|
|
|
|
@http.read_timeout = @timeout
|
|
|
|
@http.open_timeout = @timeout
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Changes the user for the Basic Authentication header to +new_user+
|
2003-07-19 06:05:54 -04:00
|
|
|
def user=(new_user)
|
|
|
|
@user = new_user
|
|
|
|
set_auth
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Changes the password for the Basic Authentication header to
|
|
|
|
# +new_password+
|
2003-07-19 06:05:54 -04:00
|
|
|
def password=(new_password)
|
|
|
|
@password = new_password
|
|
|
|
set_auth
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Invokes the method named +method+ with the parameters given by
|
|
|
|
# +args+ on the XML-RPC server.
|
|
|
|
#
|
|
|
|
# The +method+ parameter is converted into a String and should
|
|
|
|
# be a valid XML-RPC method-name.
|
|
|
|
#
|
|
|
|
# Each parameter of +args+ must be of one of the following types,
|
|
|
|
# where Hash, Struct and Array can contain any of these listed _types_:
|
|
|
|
#
|
|
|
|
# * Fixnum, Bignum
|
|
|
|
# * TrueClass, FalseClass, +true+, +false+
|
|
|
|
# * String, Symbol
|
|
|
|
# * Float
|
|
|
|
# * Hash, Struct
|
|
|
|
# * Array
|
|
|
|
# * Date, Time, XMLRPC::DateTime
|
|
|
|
# * XMLRPC::Base64
|
|
|
|
# * A Ruby object which class includes XMLRPC::Marshallable
|
|
|
|
# (only if Config::ENABLE_MARSHALLABLE is +true+).
|
|
|
|
# That object is converted into a hash, with one additional key/value
|
|
|
|
# pair <code>___class___</code> which contains the class name
|
|
|
|
# for restoring that object later.
|
|
|
|
#
|
|
|
|
# The method returns the return-value from the Remote Procedure Call.
|
|
|
|
#
|
|
|
|
# The type of the return-value is one of the types shown above.
|
|
|
|
#
|
|
|
|
# A Bignum is only allowed when it fits in 32-bit. A XML-RPC
|
|
|
|
# +dateTime.iso8601+ type is always returned as a XMLRPC::DateTime object.
|
|
|
|
# Struct is never returned, only a Hash, the same for a Symbol, where as a
|
|
|
|
# String is always returned. XMLRPC::Base64 is returned as a String from
|
|
|
|
# xmlrpc4r version 1.6.1 on.
|
|
|
|
#
|
|
|
|
# If the remote procedure returned a fault-structure, then a
|
|
|
|
# XMLRPC::FaultException exception is raised, which has two accessor-methods
|
|
|
|
# +faultCode+ an Integer, and +faultString+ a String.
|
2003-07-19 06:05:54 -04:00
|
|
|
def call(method, *args)
|
2009-03-05 22:56:38 -05:00
|
|
|
ok, param = call2(method, *args)
|
2003-07-19 06:05:54 -04:00
|
|
|
if ok
|
|
|
|
param
|
|
|
|
else
|
|
|
|
raise param
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# The difference between this method and XMLRPC::Client#call is, that
|
|
|
|
# this method will <b>NOT</b> raise a XMLRPC::FaultException exception.
|
|
|
|
#
|
|
|
|
# The method returns an array of two values. The first value indicates if
|
|
|
|
# the second value is +true+ or an XMLRPC::FaultException.
|
|
|
|
#
|
|
|
|
# Both are explained in XMLRPC::Client#call.
|
|
|
|
#
|
|
|
|
# Simple to remember: The "2" in "call2" denotes the number of values it returns.
|
2003-07-19 06:05:54 -04:00
|
|
|
def call2(method, *args)
|
|
|
|
request = create().methodCall(method, *args)
|
|
|
|
data = do_rpc(request, false)
|
|
|
|
parser().parseMethodResponse(data)
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Similar to XMLRPC::Client#call, however can be called concurrently and
|
|
|
|
# use a new connection for each request. In contrast to the corresponding
|
|
|
|
# method without the +_async+ suffix, which use connect-alive (one
|
|
|
|
# connection for all requests).
|
|
|
|
#
|
|
|
|
# Note, that you have to use Thread to call these methods concurrently.
|
|
|
|
# The following example calls two methods concurrently:
|
|
|
|
#
|
|
|
|
# Thread.new {
|
|
|
|
# p client.call_async("michael.add", 4, 5)
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# Thread.new {
|
|
|
|
# p client.call_async("michael.div", 7, 9)
|
|
|
|
# }
|
|
|
|
#
|
2003-07-19 06:05:54 -04:00
|
|
|
def call_async(method, *args)
|
2009-03-05 22:56:38 -05:00
|
|
|
ok, param = call2_async(method, *args)
|
2003-07-19 06:05:54 -04:00
|
|
|
if ok
|
|
|
|
param
|
|
|
|
else
|
|
|
|
raise param
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Same as XMLRPC::Client#call2, but can be called concurrently.
|
|
|
|
#
|
|
|
|
# See also XMLRPC::Client#call_async
|
2003-07-19 06:05:54 -04:00
|
|
|
def call2_async(method, *args)
|
|
|
|
request = create().methodCall(method, *args)
|
|
|
|
data = do_rpc(request, true)
|
|
|
|
parser().parseMethodResponse(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# You can use this method to execute several methods on a XMLRPC server
|
|
|
|
# which support the multi-call extension.
|
|
|
|
#
|
|
|
|
# s.multicall(
|
|
|
|
# ['michael.add', 3, 4],
|
|
|
|
# ['michael.sub', 4, 5]
|
|
|
|
# )
|
|
|
|
# # => [7, -1]
|
2003-07-19 06:05:54 -04:00
|
|
|
def multicall(*methods)
|
|
|
|
ok, params = multicall2(*methods)
|
|
|
|
if ok
|
|
|
|
params
|
|
|
|
else
|
|
|
|
raise params
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Same as XMLRPC::Client#multicall, but returns two parameters instead of
|
|
|
|
# raising an XMLRPC::FaultException.
|
|
|
|
#
|
|
|
|
# See XMLRPC::Client#call2
|
2003-07-19 06:05:54 -04:00
|
|
|
def multicall2(*methods)
|
|
|
|
gen_multicall(methods, false)
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Similar to XMLRPC::Client#multicall, however can be called concurrently and
|
|
|
|
# use a new connection for each request. In contrast to the corresponding
|
|
|
|
# method without the +_async+ suffix, which use connect-alive (one
|
|
|
|
# connection for all requests).
|
|
|
|
#
|
|
|
|
# Note, that you have to use Thread to call these methods concurrently.
|
|
|
|
# The following example calls two methods concurrently:
|
|
|
|
#
|
|
|
|
# Thread.new {
|
|
|
|
# p client.multicall_async("michael.add", 4, 5)
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# Thread.new {
|
|
|
|
# p client.multicall_async("michael.div", 7, 9)
|
|
|
|
# }
|
|
|
|
#
|
2003-07-19 06:05:54 -04:00
|
|
|
def multicall_async(*methods)
|
|
|
|
ok, params = multicall2_async(*methods)
|
|
|
|
if ok
|
|
|
|
params
|
|
|
|
else
|
|
|
|
raise params
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Same as XMLRPC::Client#multicall2, but can be called concurrently.
|
|
|
|
#
|
|
|
|
# See also XMLRPC::Client#multicall_async
|
2003-07-19 06:05:54 -04:00
|
|
|
def multicall2_async(*methods)
|
|
|
|
gen_multicall(methods, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Returns an object of class XMLRPC::Client::Proxy, initialized with
|
|
|
|
# +prefix+ and +args+.
|
|
|
|
#
|
|
|
|
# A proxy object returned by this method behaves like XMLRPC::Client#call,
|
|
|
|
# i.e. a call on that object will raise a XMLRPC::FaultException when a
|
|
|
|
# fault-structure is returned by that call.
|
2004-12-21 11:23:33 -05:00
|
|
|
def proxy(prefix=nil, *args)
|
2003-07-19 06:05:54 -04:00
|
|
|
Proxy.new(self, prefix, args, :call)
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Almost the same like XMLRPC::Client#proxy only that a call on the returned
|
|
|
|
# XMLRPC::Client::Proxy object will return two parameters.
|
|
|
|
#
|
|
|
|
# See XMLRPC::Client#call2
|
2004-12-21 11:23:33 -05:00
|
|
|
def proxy2(prefix=nil, *args)
|
2003-07-19 06:05:54 -04:00
|
|
|
Proxy.new(self, prefix, args, :call2)
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Similar to XMLRPC::Client#proxy, however can be called concurrently and
|
|
|
|
# use a new connection for each request. In contrast to the corresponding
|
|
|
|
# method without the +_async+ suffix, which use connect-alive (one
|
|
|
|
# connection for all requests).
|
|
|
|
#
|
|
|
|
# Note, that you have to use Thread to call these methods concurrently.
|
|
|
|
# The following example calls two methods concurrently:
|
|
|
|
#
|
|
|
|
# Thread.new {
|
|
|
|
# p client.proxy_async("michael.add", 4, 5)
|
|
|
|
# }
|
|
|
|
#
|
|
|
|
# Thread.new {
|
|
|
|
# p client.proxy_async("michael.div", 7, 9)
|
|
|
|
# }
|
|
|
|
#
|
2004-12-21 11:23:33 -05:00
|
|
|
def proxy_async(prefix=nil, *args)
|
2003-07-19 06:05:54 -04:00
|
|
|
Proxy.new(self, prefix, args, :call_async)
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Same as XMLRPC::Client#proxy2, but can be called concurrently.
|
|
|
|
#
|
|
|
|
# See also XMLRPC::Client#proxy_async
|
2004-12-21 11:23:33 -05:00
|
|
|
def proxy2_async(prefix=nil, *args)
|
2003-07-19 06:05:54 -04:00
|
|
|
Proxy.new(self, prefix, args, :call2_async)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
private
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2012-03-05 17:19:35 -05:00
|
|
|
def net_http(host, port, proxy_host, proxy_port)
|
|
|
|
Net::HTTP.new host, port, proxy_host, proxy_port
|
|
|
|
end
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
def set_auth
|
|
|
|
if @user.nil?
|
|
|
|
@auth = nil
|
|
|
|
else
|
|
|
|
a = "#@user"
|
|
|
|
a << ":#@password" if @password != nil
|
2011-07-25 09:21:49 -04:00
|
|
|
@auth = "Basic " + [a].pack("m0")
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_rpc(request, async=false)
|
2009-03-05 22:56:38 -05:00
|
|
|
header = {
|
2003-07-19 06:05:54 -04:00
|
|
|
"User-Agent" => USER_AGENT,
|
2005-10-05 12:15:16 -04:00
|
|
|
"Content-Type" => "text/xml; charset=utf-8",
|
2011-08-05 00:35:28 -04:00
|
|
|
"Content-Length" => request.bytesize.to_s,
|
2003-07-19 06:05:54 -04:00
|
|
|
"Connection" => (async ? "close" : "keep-alive")
|
|
|
|
}
|
|
|
|
|
2003-11-17 16:30:31 -05:00
|
|
|
header["Cookie"] = @cookie if @cookie
|
|
|
|
header.update(@http_header_extra) if @http_header_extra
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
if @auth != nil
|
|
|
|
# add authorization header
|
|
|
|
header["Authorization"] = @auth
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
resp = nil
|
2003-11-17 16:30:31 -05:00
|
|
|
@http_last_response = nil
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
if async
|
2009-03-05 22:56:38 -05:00
|
|
|
# use a new HTTP object for each call
|
2012-03-07 08:45:04 -05:00
|
|
|
http = net_http(@host, @port, @proxy_host, @proxy_port)
|
2003-07-19 06:05:54 -04:00
|
|
|
http.use_ssl = @use_ssl if @use_ssl
|
|
|
|
http.read_timeout = @timeout
|
|
|
|
http.open_timeout = @timeout
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
# post request
|
|
|
|
http.start {
|
2012-03-05 17:19:35 -05:00
|
|
|
resp = http.request_post(@path, request, header)
|
2003-07-19 06:05:54 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
# reuse the HTTP object for each call => connection alive is possible
|
2013-12-07 22:51:11 -05:00
|
|
|
# we must start connection explicitly first time so that http.request
|
2007-10-24 16:00:10 -04:00
|
|
|
# does not assume that we don't want keepalive
|
|
|
|
@http.start if not @http.started?
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
# post request
|
2012-03-05 17:19:35 -05:00
|
|
|
resp = @http.request_post(@path, request, header)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-11-17 16:30:31 -05:00
|
|
|
@http_last_response = resp
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
data = resp.body
|
|
|
|
|
|
|
|
if resp.code == "401"
|
|
|
|
# Authorization Required
|
2009-03-05 22:56:38 -05:00
|
|
|
raise "Authorization failed.\nHTTP-Error: #{resp.code} #{resp.message}"
|
2003-07-19 06:05:54 -04:00
|
|
|
elsif resp.code[0,1] != "2"
|
2009-03-05 22:56:38 -05:00
|
|
|
raise "HTTP-Error: #{resp.code} #{resp.message}"
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
2012-03-06 17:45:43 -05:00
|
|
|
# assume text/xml on instances where Content-Type header is not set
|
|
|
|
ct_expected = resp["Content-Type"] || 'text/xml'
|
|
|
|
ct = parse_content_type(ct_expected).first
|
2003-07-19 06:05:54 -04:00
|
|
|
if ct != "text/xml"
|
|
|
|
if ct == "text/html"
|
2007-10-24 15:48:06 -04:00
|
|
|
raise "Wrong content-type (received '#{ct}' but expected 'text/xml'): \n#{data}"
|
2003-07-19 06:05:54 -04:00
|
|
|
else
|
2007-10-24 15:48:06 -04:00
|
|
|
raise "Wrong content-type (received '#{ct}' but expected 'text/xml')"
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
expected = resp["Content-Length"] || "<unknown>"
|
2011-08-05 00:35:28 -04:00
|
|
|
if data.nil? or data.bytesize == 0
|
|
|
|
raise "Wrong size. Was #{data.bytesize}, should be #{expected}"
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
2013-06-12 07:19:18 -04:00
|
|
|
parse_set_cookies(resp.get_fields("Set-Cookie"))
|
|
|
|
|
|
|
|
return data
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_set_cookies(set_cookies)
|
2013-06-13 09:36:52 -04:00
|
|
|
return if set_cookies.nil?
|
|
|
|
return if set_cookies.empty?
|
|
|
|
require 'webrick/cookie'
|
2013-06-13 09:46:54 -04:00
|
|
|
pairs = {}
|
|
|
|
set_cookies.each do |set_cookie|
|
2013-06-13 09:36:52 -04:00
|
|
|
cookie = WEBrick::Cookie.parse_set_cookie(set_cookie)
|
2013-06-13 09:46:54 -04:00
|
|
|
pairs.delete(cookie.name)
|
|
|
|
pairs[cookie.name] = cookie.value
|
|
|
|
end
|
|
|
|
cookies = pairs.collect do |name, value|
|
|
|
|
WEBrick::Cookie.new(name, value).to_s
|
|
|
|
end
|
|
|
|
@cookie = cookies.join("; ")
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def gen_multicall(methods=[], async=false)
|
|
|
|
meth = :call2
|
|
|
|
meth = :call2_async if async
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
ok, params = self.send(meth, "system.multicall",
|
2003-07-19 06:05:54 -04:00
|
|
|
methods.collect {|m| {'methodName' => m[0], 'params' => m[1..-1]} }
|
|
|
|
)
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
if ok
|
2003-07-19 06:05:54 -04:00
|
|
|
params = params.collect do |param|
|
|
|
|
if param.is_a? Array
|
|
|
|
param[0]
|
|
|
|
elsif param.is_a? Hash
|
|
|
|
XMLRPC::FaultException.new(param["faultCode"], param["faultString"])
|
|
|
|
else
|
|
|
|
raise "Wrong multicall return value"
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return ok, params
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# XML-RPC calls look nicer!
|
|
|
|
#
|
|
|
|
# You can call any method onto objects of that class - the object handles
|
|
|
|
# XMLRPC::Client::Proxy#method_missing and will forward the method call to
|
|
|
|
# a XML-RPC server.
|
|
|
|
#
|
|
|
|
# Don't use this class directly, instead use the public instance method
|
|
|
|
# XMLRPC::Client#proxy or XMLRPC::Client#proxy2.
|
|
|
|
#
|
|
|
|
# require "xmlrpc/client"
|
|
|
|
#
|
|
|
|
# server = XMLRPC::Client.new("www.ruby-lang.org", "/RPC2", 80)
|
|
|
|
#
|
|
|
|
# michael = server.proxy("michael")
|
|
|
|
# michael2 = server.proxy("michael", 4)
|
|
|
|
#
|
|
|
|
# # both calls should return the same value '9'.
|
|
|
|
# p michael.add(4,5)
|
|
|
|
# p michael2.add(5)
|
2003-07-19 06:05:54 -04:00
|
|
|
class Proxy
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Creates an object which provides XMLRPC::Client::Proxy#method_missing.
|
|
|
|
#
|
|
|
|
# The given +server+ must be an instance of XMLRPC::Client, which is the
|
|
|
|
# XML-RPC server to be used for a XML-RPC call.
|
|
|
|
#
|
|
|
|
# +prefix+ and +delim+ will be prepended to the method name called onto this object.
|
|
|
|
#
|
|
|
|
# An optional parameter +meth+ is the method to use for a RPC.
|
|
|
|
# It can be either, call, call2, call_async, call2_async
|
|
|
|
#
|
|
|
|
# +args+ are arguments which are automatically given to every XML-RPC
|
|
|
|
# call before being provided through +method_missing+.
|
2003-07-19 06:05:54 -04:00
|
|
|
def initialize(server, prefix, args=[], meth=:call, delim=".")
|
2008-10-01 09:46:53 -04:00
|
|
|
@server = server
|
|
|
|
@prefix = prefix ? prefix + delim : ""
|
2009-03-05 22:56:38 -05:00
|
|
|
@args = args
|
2003-07-19 06:05:54 -04:00
|
|
|
@meth = meth
|
|
|
|
end
|
|
|
|
|
2012-09-12 22:22:10 -04:00
|
|
|
# Every method call is forwarded to the XML-RPC server defined in
|
|
|
|
# XMLRPC::Client::Proxy#new.
|
|
|
|
#
|
|
|
|
# Note: Inherited methods from class Object cannot be used as XML-RPC
|
|
|
|
# names, because they get around +method_missing+.
|
2003-07-19 06:05:54 -04:00
|
|
|
def method_missing(mid, *args)
|
2008-10-01 09:46:53 -04:00
|
|
|
pre = @prefix + mid.to_s
|
|
|
|
arg = @args + args
|
|
|
|
@server.send(@meth, pre, *arg)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end # class Proxy
|
|
|
|
|
|
|
|
end # class Client
|
|
|
|
|
|
|
|
end # module XMLRPC
|
|
|
|
|