Class: XMLRPC::WEBrickServlet

Inherits:
BasicServer show all
Defined in:
lib/xmlrpc/server.rb,
lib/xmlrpc/server.rb

Overview

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>))

Direct Known Subclasses

Server

Constant Summary

Constants inherited from BasicServer

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

Instance Method Summary collapse

Methods inherited from BasicServer

#add_handler, #add_introspection, #add_multicall, #get_default_handler, #get_service_hook, #process, #set_default_handler, #set_service_hook

Methods included from ParseContentType

#parse_content_type

Methods included from ParserWriterChooseMixin

#set_parser, #set_writer

Constructor Details

#initialize(*a) ⇒ WEBrickServlet

Returns a new instance of WEBrickServlet.



704
705
706
707
708
# File 'lib/xmlrpc/server.rb', line 704

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

Instance Method Details

#get_instance(config, *options) ⇒ Object



716
717
718
719
# File 'lib/xmlrpc/server.rb', line 716

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

#get_valid_ipObject



729
730
731
# File 'lib/xmlrpc/server.rb', line 729

def get_valid_ip
  @valid_ip
end

#require_path_info?Boolean

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

Returns:

  • (Boolean)


712
713
714
# File 'lib/xmlrpc/server.rb', line 712

def require_path_info?
  false
end

#service(request, response) ⇒ Object

Raises:

  • (WEBrick::HTTPStatus::LengthRequired)


733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
# File 'lib/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) ⇒ Object



721
722
723
724
725
726
727
# File 'lib/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