123456789_123456789_123456789_123456789_123456789_

Class: XMLRPC::WEBrickServlet

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, BasicServer
Instance Chain:
Inherits: XMLRPC::BasicServer
Defined in: lib/xmlrpc/server.rb,
lib/xmlrpc/server.rb

Overview

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

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

Constant Summary

BasicServer - Inherited

ERR_MC_EXPECTED_STRUCT, ERR_MC_MISSING_METHNAME, ERR_MC_MISSING_PARAMS, ERR_MC_RECURSIVE_CALL, ERR_MC_WRONG_PARAM, ERR_MC_WRONG_PARAM_PARAMS, ERR_METHOD_MISSING, ERR_UNCAUGHT_EXCEPTION

Class Method Summary

BasicServer - Inherited

.new

Creates a new BasicServer instance, which should not be done, because BasicServer is an abstract class.

Instance Attribute Summary

Instance Method Summary

BasicServer - Inherited

#add_handler

Adds aBlock to the list of handlers, with name as the name of the method.

#add_introspection

Adds the introspection handlers "system.listMethods", "system.methodSignature" and "system.methodHelp", where only the first one works.

#add_multicall

Adds the multi-call handler "system.multicall".

#get_default_handler

Returns the default-handler, which is called when no handler for a method-name is found.

#get_service_hook

Returns the service-hook, which is called on each service request (RPC) unless it's nil.

#process,
#set_default_handler

Sets handler as the default-handler, which is called when no handler for a method-name is found.

#set_service_hook

A service-hook is called for each service request (RPC).

#call_method,
#check_arity

Returns true, if the arity of obj matches n_args

#dispatch, #handle, #multicall_fault

ParseContentType - Included

ParserWriterChooseMixin - Included

#set_parser

Sets the XMLParser to use for parsing XML documents.

#set_writer

Sets the XMLWriter to use for generating XML output.

#create, #parser

Constructor Details

.new(*a) ⇒ WEBrickServlet

[ GitHub ]

  
# File 'lib/xmlrpc/server.rb', line 628

def initialize(*a)
  super
  require "webrick/httpstatus"
  @valid_ip = nil
end

Instance Attribute Details

#require_path_info?Boolean (readonly)

Deprecated from WEBrick/1.2.2, but does not break anything.

[ GitHub ]

  
# File 'lib/xmlrpc/server.rb', line 635

def require_path_info?
  false
end

Instance Method Details

#get_instance(config, *options)

[ GitHub ]

  
# File 'lib/xmlrpc/server.rb', line 639

def get_instance(config, *options)
  # TODO: set config & options
  self
end

#get_valid_ip

Return the valid IP addresses that are allowed to connect to the server.

See also, XMLRPC::Server#set_valid_ip

[ GitHub ]

  
# File 'lib/xmlrpc/server.rb', line 658

def get_valid_ip
  @valid_ip
end

#service(request, response)

Raises:

  • (WEBrick::HTTPStatus::LengthRequired)
[ GitHub ]

  
# File 'lib/xmlrpc/server.rb', line 662

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)

Specifies the valid IP addresses that are allowed to connect to the server.

Each IP is either a String or a Regexp.

[ GitHub ]

  
# File 'lib/xmlrpc/server.rb', line 647

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