2003-07-19 06:05:54 -04:00
|
|
|
=begin
|
|
|
|
= xmlrpc/client.rb
|
|
|
|
Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
|
|
|
|
|
|
|
|
Released under the same term of license as Ruby.
|
|
|
|
|
|
|
|
= Classes
|
|
|
|
* ((<XMLRPC::Client>))
|
|
|
|
* ((<XMLRPC::Client::Proxy>))
|
|
|
|
|
|
|
|
|
|
|
|
= XMLRPC::Client
|
|
|
|
== Synopsis
|
|
|
|
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"
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
== Description
|
|
|
|
Class (({XMLRPC::Client})) provides remote procedure calls to a XML-RPC server.
|
|
|
|
After setting the connection-parameters with ((<XMLRPC::Client.new>)) which
|
2009-03-05 22:56:38 -05:00
|
|
|
creates a new (({XMLRPC::Client})) instance, you can execute a remote procedure
|
2003-07-19 06:05:54 -04:00
|
|
|
by sending the ((<call|XMLRPC::Client#call>)) or ((<call2|XMLRPC::Client#call2>))
|
2009-03-05 22:56:38 -05:00
|
|
|
message to this new instance. The given parameters indicate which method to
|
2003-07-19 06:05:54 -04:00
|
|
|
call on the remote-side and of course the parameters for the remote procedure.
|
|
|
|
|
|
|
|
== Class Methods
|
|
|
|
--- XMLRPC::Client.new( host=nil, path=nil, port=nil, proxy_host=nil, proxy_port=nil, user=nil, password=nil, use_ssl=false, timeout =nil)
|
2009-03-05 22:56:38 -05:00
|
|
|
Creates an object which represents the remote XML-RPC server on the
|
2003-07-19 06:05:54 -04:00
|
|
|
given host ((|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 (({"/RPC2"})).
|
|
|
|
((|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'.
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
If ((|user|)) and ((|password|)) are given, each time a request is send,
|
|
|
|
a Authorization header is send. Currently only Basic Authentification is
|
2003-07-19 06:05:54 -04:00
|
|
|
implemented no Digest.
|
|
|
|
|
|
|
|
If ((|use_ssl|)) is set to (({true})), comunication over SSL is enabled.
|
|
|
|
Note, that you need the SSL package from RAA installed.
|
|
|
|
|
|
|
|
Parameter ((|timeout|)) is the time to wait for a XML-RPC response, defaults to 30.
|
|
|
|
|
|
|
|
--- XMLRPC::Client.new2( uri, proxy=nil, timeout=nil)
|
2005-06-19 11:47:31 -04:00
|
|
|
--- XMLRPC::Client.new_from_uri( uri, proxy=nil, timeout=nil)
|
2003-07-19 06:05:54 -04:00
|
|
|
: uri
|
|
|
|
URI specifying protocol (http or https), host, port, path, user and password.
|
|
|
|
Example: https://user:password@host:port/path
|
|
|
|
|
|
|
|
: proxy
|
|
|
|
Is of the form "host:port".
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
: timeout
|
2009-03-05 22:56:38 -05:00
|
|
|
Defaults to 30.
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
--- XMLRPC::Client.new3( hash={} )
|
2005-06-19 11:47:31 -04:00
|
|
|
--- XMLRPC::Client.new_from_hash( hash={} )
|
2003-07-19 06:05:54 -04:00
|
|
|
Parameter ((|hash|)) has following case-insensitive keys:
|
|
|
|
* host
|
|
|
|
* path
|
|
|
|
* port
|
|
|
|
* proxy_host
|
|
|
|
* proxy_port
|
|
|
|
* user
|
|
|
|
* password
|
|
|
|
* use_ssl
|
|
|
|
* timeout
|
|
|
|
|
|
|
|
Calls ((<XMLRPC::Client.new>)) with the corresponding values.
|
|
|
|
|
|
|
|
== Instance Methods
|
|
|
|
--- XMLRPC::Client#call( method, *args )
|
2009-03-05 22:56:38 -05:00
|
|
|
Invokes the method named ((|method|)) with the parameters given by
|
2003-07-19 06:05:54 -04:00
|
|
|
((|args|)) on the XML-RPC server.
|
2009-03-05 22:56:38 -05:00
|
|
|
The parameter ((|method|)) is converted into a (({String})) and should
|
|
|
|
be a valid XML-RPC method-name.
|
2003-07-19 06:05:54 -04:00
|
|
|
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}))
|
2009-03-05 22:56:38 -05:00
|
|
|
* (({XMLRPC::Base64}))
|
|
|
|
* A Ruby object which class includes XMLRPC::Marshallable (only if Config::ENABLE_MARSHALLABLE is (({true}))).
|
2003-07-19 06:05:54 -04:00
|
|
|
That object is converted into a hash, with one additional key/value pair "___class___" which contains the class name
|
|
|
|
for restoring later that object.
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
|
|
The method returns the return-value from the RPC
|
|
|
|
((-stands for Remote Procedure Call-)).
|
2003-07-19 06:05:54 -04:00
|
|
|
The type of the return-value is one of the above shown,
|
|
|
|
only that a (({Bignum})) is only allowed when it fits in 32-bit and
|
|
|
|
that a XML-RPC (('dateTime.iso8601')) type is always returned as
|
2009-03-05 22:56:38 -05:00
|
|
|
a ((<(({XMLRPC::DateTime}))|URL:datetime.html>)) object and
|
2003-07-19 06:05:54 -04:00
|
|
|
a (({Struct})) is never returned, only a (({Hash})), the same for a (({Symbol})), where
|
2009-03-05 22:56:38 -05:00
|
|
|
always a (({String})) is returned.
|
2003-07-19 06:05:54 -04:00
|
|
|
A (({XMLRPC::Base64})) is returned as a (({String})) from xmlrpc4r version 1.6.1 on.
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
|
|
If the remote procedure returned a fault-structure, then a
|
2003-07-19 06:05:54 -04:00
|
|
|
(({XMLRPC::FaultException})) exception is raised, which has two accessor-methods
|
|
|
|
(({faultCode})) and (({faultString})) of type (({Integer})) and (({String})).
|
|
|
|
|
|
|
|
--- XMLRPC::Client#call2( method, *args )
|
|
|
|
The difference between this method and ((<call|XMLRPC::Client#call>)) is, that
|
|
|
|
this method do ((*not*)) raise a (({XMLRPC::FaultException})) exception.
|
2009-03-05 22:56:38 -05:00
|
|
|
The method returns an array of two values. The first value indicates if
|
2003-07-19 06:05:54 -04:00
|
|
|
the second value is a return-value ((({true}))) or an object of type
|
2009-03-05 22:56:38 -05:00
|
|
|
(({XMLRPC::FaultException})).
|
2003-07-19 06:05:54 -04:00
|
|
|
Both are explained in ((<call|XMLRPC::Client#call>)).
|
|
|
|
|
2005-06-19 11:47:31 -04:00
|
|
|
Simple to remember: The "2" in "call2" denotes the number of values it returns.
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
--- XMLRPC::Client#multicall( *methods )
|
|
|
|
You can use this method to execute several methods on a XMLRPC server which supports
|
|
|
|
the multi-call extension.
|
|
|
|
Example:
|
|
|
|
|
|
|
|
s.multicall(
|
|
|
|
['michael.add', 3, 4],
|
|
|
|
['michael.sub', 4, 5]
|
|
|
|
)
|
|
|
|
# => [7, -1]
|
|
|
|
|
|
|
|
--- XMLRPC::Client#multicall2( *methods )
|
2009-03-05 22:56:38 -05:00
|
|
|
Same as ((<XMLRPC::Client#multicall>)), but returns like ((<XMLRPC::Client#call2>)) two parameters
|
2003-07-19 06:05:54 -04:00
|
|
|
instead of raising an (({XMLRPC::FaultException})).
|
|
|
|
|
|
|
|
--- XMLRPC::Client#proxy( prefix, *args )
|
|
|
|
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
|
2009-03-05 22:56:38 -05:00
|
|
|
(({XMLRPC::FaultException})) when a fault-structure is returned by that call.
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
--- XMLRPC::Client#proxy2( prefix, *args )
|
|
|
|
Almost the same like ((<XMLRPC::Client#proxy>)) only that a call on the returned
|
|
|
|
(({XMLRPC::Client::Proxy})) object behaves like ((<XMLRPC::Client#call2>)), i.e.
|
2009-03-05 22:56:38 -05:00
|
|
|
a call on that object will return two parameters.
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
--- XMLRPC::Client#call_async(...)
|
|
|
|
--- XMLRPC::Client#call2_async(...)
|
|
|
|
--- XMLRPC::Client#multicall_async(...)
|
|
|
|
--- XMLRPC::Client#multicall2_async(...)
|
|
|
|
--- XMLRPC::Client#proxy_async(...)
|
|
|
|
--- XMLRPC::Client#proxy2_async(...)
|
|
|
|
In contrast to corresponding methods without "_async", these can be
|
2009-03-05 22:56:38 -05:00
|
|
|
called concurrently and use for each request a new connection, where the
|
2003-07-19 06:05:54 -04:00
|
|
|
non-asynchronous counterparts use connection-alive (one connection for all requests)
|
2009-03-05 22:56:38 -05:00
|
|
|
if possible.
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
Note, that you have to use Threads to call these methods concurrently.
|
2003-07-19 06:05:54 -04:00
|
|
|
The following example calls two methods concurrently:
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
Thread.new {
|
|
|
|
p client.call_async("michael.add", 4, 5)
|
|
|
|
}
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
Thread.new {
|
|
|
|
p client.call_async("michael.div", 7, 9)
|
|
|
|
}
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
--- XMLRPC::Client#timeout
|
|
|
|
--- XMLRPC::Client#user
|
|
|
|
--- XMLRPC::Client#password
|
|
|
|
Return the corresponding attributes.
|
|
|
|
|
|
|
|
--- XMLRPC::Client#timeout= (new_timeout)
|
|
|
|
--- XMLRPC::Client#user= (new_user)
|
|
|
|
--- XMLRPC::Client#password= (new_password)
|
|
|
|
Set the corresponding attributes.
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
--- XMLRPC::Client#set_writer( writer )
|
|
|
|
Sets the XML writer to use for generating XML output.
|
|
|
|
Should be an instance of a class from module (({XMLRPC::XMLWriter})).
|
2009-03-05 22:56:38 -05:00
|
|
|
If this method is not called, then (({XMLRPC::Config::DEFAULT_WRITER})) is used.
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
--- XMLRPC::Client#set_parser( parser )
|
|
|
|
Sets the XML parser to use for parsing XML documents.
|
|
|
|
Should be an instance of a class from module (({XMLRPC::XMLParser})).
|
|
|
|
If this method is not called, then (({XMLRPC::Config::DEFAULT_PARSER})) is used.
|
|
|
|
|
2003-11-17 16:30:31 -05:00
|
|
|
--- XMLRPC::Client#cookie
|
|
|
|
--- XMLRPC::Client#cookie= (cookieString)
|
|
|
|
Get and set the HTTP Cookie header.
|
|
|
|
|
|
|
|
--- XMLRPC::Client#http_header_extra= (additionalHeaders)
|
|
|
|
Set extra HTTP headers that are included in the request.
|
|
|
|
|
|
|
|
--- XMLRPC::Client#http_header_extra
|
2009-03-05 22:56:38 -05:00
|
|
|
Access the via ((<XMLRPC::Client#http_header_extra=>)) assigned header.
|
2003-11-17 16:30:31 -05:00
|
|
|
|
|
|
|
--- XMLRPC::Client#http_last_response
|
|
|
|
Returns the (({Net::HTTPResponse})) object of the last RPC.
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
= XMLRPC::Client::Proxy
|
|
|
|
== Synopsis
|
|
|
|
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)
|
|
|
|
|
|
|
|
== Description
|
|
|
|
Class (({XMLRPC::Client::Proxy})) makes XML-RPC calls look nicer!
|
2009-03-05 22:56:38 -05:00
|
|
|
You can call any method onto objects of that class - the object handles
|
2003-07-19 06:05:54 -04:00
|
|
|
(({method_missing})) and will forward the method call to a XML-RPC server.
|
|
|
|
Don't use this class directly, but use instead method ((<XMLRPC::Client#proxy>)) or
|
|
|
|
((<XMLRPC::Client#proxy2>)).
|
|
|
|
|
|
|
|
== Class Methods
|
2009-03-05 22:56:38 -05:00
|
|
|
--- XMLRPC::Client::Proxy.new( server, prefix, args=[], meth=:call, delim="." )
|
2003-07-19 06:05:54 -04:00
|
|
|
Creates an object which provides (({method_missing})).
|
|
|
|
|
|
|
|
((|server|)) must be of type (({XMLRPC::Client})), which is the XML-RPC server to be used
|
|
|
|
for a XML-RPC call. ((|prefix|)) and ((|delim|)) will be prepended to the methodname
|
2009-03-05 22:56:38 -05:00
|
|
|
called onto this object.
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
Parameter ((|meth|)) is the method (call, call2, call_async, call2_async) to use for
|
|
|
|
a RPC.
|
|
|
|
|
|
|
|
((|args|)) are arguments which are automatically given
|
|
|
|
to every XML-RPC call before the arguments provides through (({method_missing})).
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
== Instance Methods
|
|
|
|
Every method call is forwarded to the XML-RPC server defined in ((<new|XMLRPC::Client::Proxy#new>)).
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
Note: Inherited methods from class (({Object})) cannot be used as XML-RPC names, because they get around
|
2009-03-05 22:56:38 -05:00
|
|
|
(({method_missing})).
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
= History
|
|
|
|
$Id$
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
require "xmlrpc/parser"
|
|
|
|
require "xmlrpc/create"
|
|
|
|
require "xmlrpc/config"
|
|
|
|
require "xmlrpc/utils" # ParserWriterChooseMixin
|
|
|
|
require "net/http"
|
|
|
|
|
|
|
|
module XMLRPC
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
# Constructors -------------------------------------------------------------------
|
|
|
|
|
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
|
|
|
|
Net::HTTP.version_1_2
|
2009-03-05 22:56:38 -05:00
|
|
|
@http = Net::HTTP.new(@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
|
|
|
|
|
|
|
|
def new2(uri, proxy=nil, timeout=nil)
|
2003-07-19 06:05:54 -04:00
|
|
|
if match = /^([^:]+):\/\/(([^@]+)@)?([^\/]+)(\/.*)?$/.match(uri)
|
|
|
|
proto = match[1]
|
|
|
|
user, passwd = (match[3] || "").split(":")
|
2009-03-05 22:56:38 -05:00
|
|
|
host, port = match[4].split(":")
|
2003-07-19 06:05:54 -04:00
|
|
|
path = match[5]
|
|
|
|
|
|
|
|
if proto != "http" and proto != "https"
|
|
|
|
raise "Wrong protocol specified. Only http or https allowed!"
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
raise "Wrong URI as parameter!"
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
proxy_host, proxy_port = (proxy || "").split(":")
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
# Attribute Accessors -------------------------------------------------------------------
|
|
|
|
|
2003-11-17 16:30:31 -05:00
|
|
|
# add additional HTTP headers to the request
|
|
|
|
attr_accessor :http_header_extra
|
|
|
|
|
|
|
|
# makes last HTTP response accessible
|
|
|
|
attr_reader :http_last_response
|
|
|
|
|
|
|
|
# Cookie support
|
|
|
|
attr_accessor :cookie
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-11-17 16:30:31 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
attr_reader :timeout, :user, :password
|
|
|
|
|
|
|
|
def timeout=(new_timeout)
|
|
|
|
@timeout = new_timeout
|
|
|
|
@http.read_timeout = @timeout
|
|
|
|
@http.open_timeout = @timeout
|
|
|
|
end
|
|
|
|
|
|
|
|
def user=(new_user)
|
|
|
|
@user = new_user
|
|
|
|
set_auth
|
|
|
|
end
|
|
|
|
|
|
|
|
def password=(new_password)
|
|
|
|
@password = new_password
|
|
|
|
set_auth
|
|
|
|
end
|
|
|
|
|
|
|
|
# Call methods --------------------------------------------------------------
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def call2(method, *args)
|
|
|
|
request = create().methodCall(method, *args)
|
|
|
|
data = do_rpc(request, false)
|
|
|
|
parser().parseMethodResponse(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
def call2_async(method, *args)
|
|
|
|
request = create().methodCall(method, *args)
|
|
|
|
data = do_rpc(request, true)
|
|
|
|
parser().parseMethodResponse(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Multicall methods --------------------------------------------------------------
|
|
|
|
|
|
|
|
def multicall(*methods)
|
|
|
|
ok, params = multicall2(*methods)
|
|
|
|
if ok
|
|
|
|
params
|
|
|
|
else
|
|
|
|
raise params
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def multicall2(*methods)
|
|
|
|
gen_multicall(methods, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def multicall_async(*methods)
|
|
|
|
ok, params = multicall2_async(*methods)
|
|
|
|
if ok
|
|
|
|
params
|
|
|
|
else
|
|
|
|
raise params
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def multicall2_async(*methods)
|
|
|
|
gen_multicall(methods, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Proxy generating methods ------------------------------------------
|
2009-03-05 22:56:38 -05:00
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
private # ----------------------------------------------------------
|
|
|
|
|
|
|
|
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
|
2003-07-19 06:05:54 -04:00
|
|
|
Net::HTTP.version_1_2
|
2009-03-05 22:56:38 -05:00
|
|
|
http = Net::HTTP.new(@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 {
|
2009-03-05 22:56:38 -05:00
|
|
|
resp = http.post2(@path, request, header)
|
2003-07-19 06:05:54 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
# reuse the HTTP object for each call => connection alive is possible
|
2007-10-24 16:00:10 -04:00
|
|
|
# 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?
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
# post request
|
2009-03-05 22:56:38 -05:00
|
|
|
resp = @http.post2(@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
|
|
|
|
|
|
|
|
ct = parse_content_type(resp["Content-Type"]).first
|
|
|
|
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}"
|
|
|
|
elsif expected != "<unknown>" and expected.to_i != data.bytesize and resp["Transfer-Encoding"].nil?
|
|
|
|
raise "Wrong size. Was #{data.bytesize}, should be #{expected}"
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
2008-04-15 10:10:18 -04:00
|
|
|
set_cookies = resp.get_fields("Set-Cookie")
|
|
|
|
if set_cookies and !set_cookies.empty?
|
2008-07-12 03:02:40 -04:00
|
|
|
require 'webrick/cookie'
|
2008-04-15 10:10:18 -04:00
|
|
|
@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
|
2003-11-17 16:30:31 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
return data
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Proxy
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|