In Files

XMLRPC::WEBrickServlet

XMLRPC::Server

Synopsis

require "xmlrpc/server"

s = XMLRPC::Server.new(8080)

s.add_handler("michael.add") do |a,b|
  a + b
end

s.add_handler("michael.div") do |a,b|
  if b == 0
    raise XMLRPC::FaultException.new(1, "division by zero")
  else
    a / b
  end
end

s.set_default_handler do |name, *args|
  raise XMLRPC::FaultException.new(-99, "Method #{name} missing" +
                                   " or wrong number of parameters!")
end

s.serve

Description

Implements a standalone XML-RPC server. The method (({serve}))) is left if a SIGHUP is sent to the program.

Superclass

((<XMLRPC::WEBrickServlet>))

Class Methods

XMLRPC::Server.new( port=8080, host=“127.0.0.1”, maxConnections=4, stdlog=$stdout, audit=true, debug=true, *a )

Creates a new (({XMLRPC::Server})) instance, which is a XML-RPC server listening on
port ((|port|)) and accepts requests for the host ((|host|)), which is by default only the localhost.
The server is not started, to start it you have to call ((<serve|XMLRPC::Server#serve>)).

Parameters ((|audit|)) and ((|debug|)) are obsolete!

All additionally given parameters in ((|*a|)) are by-passed to ((<XMLRPC::BasicServer.new>)).

Instance Methods

XMLRPC::Server#serve

Call this after you have added all you handlers to the server.
This method starts the server to listen for XML-RPC requests and answer them.

XMLRPC::Server#shutdown

Stops and shuts the server down.

XMLRPC::WEBrickServlet

Synopsis

require "webrick"
require "xmlrpc/server"

s = XMLRPC::WEBrickServlet.new
s.add_handler("michael.add") do |a,b|
  a + b
end

s.add_handler("michael.div") do |a,b|
  if b == 0
    raise XMLRPC::FaultException.new(1, "division by zero")
  else
    a / b
  end
end

s.set_default_handler do |name, *args|
  raise XMLRPC::FaultException.new(-99, "Method #{name} missing" +
                                   " or wrong number of parameters!")
end

httpserver = WEBrick::HTTPServer.new(:Port => 8080)
httpserver.mount("/RPC2", s)
trap("HUP") { httpserver.shutdown }   # use 1 instead of "HUP" on Windows
httpserver.start

Instance Methods

XMLRPC::WEBrickServlet#set_valid_ip( *ip_addr )

Specifies the valid IP addresses that are allowed to connect to the server.
Each IP is either a (({String})) or a (({Regexp})).

XMLRPC::WEBrickServlet#get_valid_ip

Return the via method ((<set_valid_ip|XMLRPC::Server#set_valid_ip>)) specified
valid IP addresses.

Description

Implements a servlet for use with WEBrick, a pure Ruby (HTTP-) server framework.

Superclass

((<XMLRPC::BasicServer>))

Public Class Methods

new(*a) click to toggle source
               # File xmlrpc/server.rb, line 704
def initialize(*a)
  super
  require "webrick/httpstatus"
  @valid_ip = nil
end
            

Public Instance Methods

get_instance(config, *options) click to toggle source
               # File xmlrpc/server.rb, line 716
def get_instance(config, *options)
  # TODO: set config & options
  self
end
            
get_valid_ip() click to toggle source
               # File xmlrpc/server.rb, line 729
def get_valid_ip
  @valid_ip
end
            
require_path_info?() click to toggle source

deprecated from WEBrick/1.2.2. but does not break anything.

               # File xmlrpc/server.rb, line 712
def require_path_info?
  false
end
            
service(request, response) click to toggle source
               # File xmlrpc/server.rb, line 733
def service(request, response)

  if @valid_ip
    raise WEBrick::HTTPStatus::Forbidden unless @valid_ip.any? { |ip| request.peeraddr[3] =~ ip }
  end

  if request.request_method != "POST"
    raise WEBrick::HTTPStatus::MethodNotAllowed,
          "unsupported method `#{request.request_method}'."
  end

  if parse_content_type(request['Content-type']).first != "text/xml"
    raise WEBrick::HTTPStatus::BadRequest
  end

  length = (request['Content-length'] || 0).to_i

  raise WEBrick::HTTPStatus::LengthRequired unless length > 0

  data = request.body

  if data.nil? or data.bytesize != length
    raise WEBrick::HTTPStatus::BadRequest
  end

  resp = process(data)
  if resp.nil? or resp.bytesize <= 0
    raise WEBrick::HTTPStatus::InternalServerError
  end

  response.status = 200
  response['Content-Length'] = resp.bytesize
  response['Content-Type']   = "text/xml; charset=utf-8"
  response.body = resp
end
            
set_valid_ip(*ip_addr) click to toggle source
               # File xmlrpc/server.rb, line 721
def set_valid_ip(*ip_addr)
  if ip_addr.size == 1 and ip_addr[0].nil?
    @valid_ip = nil
  else
    @valid_ip = ip_addr
  end
end
            

Commenting is here to help enhance the documentation. For example, sample code, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

blog comments powered by Disqus