In Files

  • xmlrpc/base64.rb
  • xmlrpc/client.rb
  • xmlrpc/config.rb
  • xmlrpc/create.rb
  • xmlrpc/datetime.rb
  • xmlrpc/marshal.rb
  • xmlrpc/parser.rb
  • xmlrpc/server.rb
  • xmlrpc/utils.rb

XMLRPC

begin

xmlrpc/base64.rb

Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)

Released under the same term of license as Ruby.

Classes

  • ((<XMLRPC::Base64>))

XMLRPC::Base64

Description

This class is necessary for (('xmlrpc4r')) to determine that a string should be transmitted base64-encoded and not as a raw-string. You can use (({XMLRPC::Base64})) on the client and server-side as a parameter and/or return-value.

Class Methods

$Id: config.rb 11708 2007-02-12 23:01:19Z shyouhei $ Configuration file for XML-RPC for Ruby

Defines ParserWriterChooseMixin, which makes it possible to choose a different XML writer and/or XML parser then the default one. The Mixin is used in client.rb (class Client) and server.rb (class BasicServer)

Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)

$Id: utils.rb 16907 2008-06-07 16:54:15Z shyouhei $

Public Instance Methods

call_method(methodname, *args) click to toggle source
 
               # File xmlrpc/server.rb, line 366
def call_method(methodname, *args)
  begin
    [true, dispatch(methodname, *args)]
  rescue XMLRPC::FaultException => e  
    [false, e]  
  rescue Exception => e
    [false, XMLRPC::FaultException.new(ERR_UNCAUGHT_EXCEPTION, "Uncaught exception #{e.message} in method #{methodname}")]
  end
end
            
check_arity(obj, n_args) click to toggle source

returns true, if the arity of “obj” matches

 
               # File xmlrpc/server.rb, line 354
def check_arity(obj, n_args)
  ary = obj.arity

  if ary >= 0
    n_args == ary
  else
    n_args >= (ary+1).abs 
  end
end
            
dispatch(methodname, *args) click to toggle source

method dispatch

 
               # File xmlrpc/server.rb, line 324
def dispatch(methodname, *args)
  for name, obj in @handler
    if obj.kind_of? Proc
      next unless methodname == name
    else
      next unless methodname =~ /^#{name}(.+)$/
      next unless obj.respond_to? $1
      obj = obj.method($1)
    end

    if check_arity(obj, args.size)
      if @service_hook.nil?
        return obj.call(*args) 
      else
        return @service_hook.call(obj, *args)
      end
    end
  end 
 
  if @default_handler.nil?
    raise XMLRPC::FaultException.new(ERR_METHOD_MISSING, "Method #{methodname} missing or wrong number of parameters!")
  else
    @default_handler.call(methodname, *args) 
  end
end
            
do_rpc(request, async=false) click to toggle source
 
               # File xmlrpc/client.rb, line 500
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
 
  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
    # 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
  
  @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

  c = resp["Set-Cookie"]
  @cookie = c if c

  return data
end
            
gen_multicall(methods=[], async=false) click to toggle source
 
               # File xmlrpc/client.rb, line 574
def gen_multicall(methods=[], async=false)
  meth = :call2
  meth = :call2_async if async

  ok, params = self.send(meth, "system.multicall", 
    methods.collect {|m| {'methodName' => m[0], 'params' => m[1..-1]} }
  )

  if ok 
    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"
      end 
    end
  end

  return ok, params
end
            
handle(methodname, *args) click to toggle source
 
               # File xmlrpc/server.rb, line 379
def handle(methodname, *args)
  create().methodResponse(*call_method(methodname, *args))
end