Class: XMLRPC::WEBrickServlet (Ruby 2.2.4)
Description: Class : XMLRPC::WEBrickServlet - Ruby 2.2.4

- Home
- Core 2.2.4
- Std-lib 2.2.4
- Downloads

Support for the Ruby 2.2 series has ended. See here for reference.

### Home Classes Methods

### In Files

- xmlrpc/server.rb

### Parent

XMLRPC::BasicServer

### Methods

- ::new
- #get_instance
- #get_valid_ip
- #require_path_info?
- #service
- #set_valid_ip

### Class/Module Index

Quicksearch

- NQXML
- NQXML::Node
- XMLRPC
- XMLRPC::Base64
- XMLRPC::BasicServer
- XMLRPC::CGIServer
- XMLRPC::Client
- XMLRPC::Client::Proxy
- XMLRPC::Config
- XMLRPC::Convert
- XMLRPC::Create
- XMLRPC::DateTime
- XMLRPC::FaultException
- XMLRPC::Marshal
- XMLRPC::Marshallable
- XMLRPC::ModRubyServer
- XMLRPC::ParseContentType
- XMLRPC::ParserWriterChooseMixin
- XMLRPC::Server
- XMLRPC::Service
- XMLRPC::Service::BasicInterface
- XMLRPC::Service::Interface
- XMLRPC::Service::PublicInstanceMethodsInterface
- XMLRPC::WEBrickServlet
- XMLRPC::XMLParser
- XMLRPC::XMLParser::AbstractStreamParser
- XMLRPC::XMLParser::AbstractTreeParser
- XMLRPC::XMLParser::LibXMLStreamParser
- XMLRPC::XMLParser::LibXMLStreamParser::LibXMLStreamListener
- XMLRPC::XMLParser::NQXMLParser
- XMLRPC::XMLParser::NQXMLStreamParser
- XMLRPC::XMLParser::NQXMLStreamParser::XMLRPCParser
- XMLRPC::XMLParser::NQXMLTreeParser
- XMLRPC::XMLParser::REXMLStreamParser
- XMLRPC::XMLParser::REXMLStreamParser::StreamListener
- XMLRPC::XMLParser::StreamParserMixin
- XMLRPC::XMLParser::XMLParser
- XMLRPC::XMLParser::XMLScanStreamParser
- XMLRPC::XMLParser::XMLScanStreamParser::XMLScanParser
- XMLRPC::XMLParser::XMLStreamParser
- XMLRPC::XMLParser::XMLTreeParser
- XMLRPC::XMLWriter
- XMLRPC::XMLWriter::Abstract
- XMLRPC::XMLWriter::Simple
- XMLRPC::XMLWriter::XMLParser

No matching classes.

# XMLRPC::WEBrickServlet

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

### Public Class Methods

new(*a) click to toggle source

                   # File xmlrpc/server.rb, line 628
    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 639
    def get_instance(config, *options)
      # TODO: set config & options
      self
    end

get_valid_ip() click to toggle source

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

See also, #set_valid_ip

                   # File xmlrpc/server.rb, line 658
    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 635
    def require_path_info?
      false
    end

service(request, response) click to toggle source

                   # File 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) click to toggle source

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

Each IP is either a String or a Regexp.

                   # File 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