class WEBrick::HTTPServlet::AbstractServlet

AbstractServlet allows HTTP server modules to be reused across multiple servers and allows encapsulation of functionality.

By default a servlet will respond to GET, HEAD (through an alias to GET) and OPTIONS requests.

By default a new servlet is initialized for every request. A servlet instance can be reused by overriding ::get_instance in the AbstractServlet subclass.

A Simple Servlet

class Simple < WEBrick::HTTPServlet::AbstractServlet
  def do_GET request, response
    status, content_type, body = do_stuff_with request

    response.status = status
    response['Content-Type'] = content_type
    response.body = body

  def do_stuff_with request
    return 200, 'text/plain', 'you got a page'

This servlet can be mounted on a server at a given path:

server.mount '/simple', Simple

Servlet Configuration

Servlets can be configured via initialize. The first argument is the HTTP server the servlet is being initialized for.

class Configurable < Simple
  def initialize server, color, size
    super server
    @color = color
    @size = size

  def do_stuff_with request
    content = "<p " \
              %q{style="color: #{@color}; font-size: #{@size}"} \
              ">Hello, World!"

    return 200, "text/html", content

This servlet must be provided two arguments at mount time:

server.mount '/configurable', Configurable, 'red', '2em'

Public Class Methods

get_instance(server, *options) click to toggle source

Factory for servlet instances that will handle a request from server using options from the mount point. By default a new servlet instance is created for every call.

# File lib/webrick/httpservlet/abstract.rb, line 84
def self.get_instance(server, *options), *options)
new(server, *options) click to toggle source

Initializes a new servlet for server using options which are stored as-is in +@options+. +@logger+ is also provided.

# File lib/webrick/httpservlet/abstract.rb, line 92
def initialize(server, *options)
  @server = @config = server
  @logger = @server[:Logger]
  @options = options

Public Instance Methods

do_GET(req, res) click to toggle source

Raises a NotFound exception

# File lib/webrick/httpservlet/abstract.rb, line 116
def do_GET(req, res)
  raise HTTPStatus::NotFound, "not found."
do_HEAD(req, res) click to toggle source

Dispatches to #do_GET

# File lib/webrick/httpservlet/abstract.rb, line 123
def do_HEAD(req, res)
  do_GET(req, res)
do_OPTIONS(req, res) click to toggle source

Returns the allowed HTTP request methods

# File lib/webrick/httpservlet/abstract.rb, line 130
def do_OPTIONS(req, res)
  m = self.methods.grep(/\Ado_([A-Z]+)\z/) {$1}
  res["allow"] = m.join(",")
service(req, res) click to toggle source

Dispatches to a do_ method based on req if such a method is available. (do_GET for a GET request). Raises a MethodNotAllowed exception if the method is not implemented.

# File lib/webrick/httpservlet/abstract.rb, line 103
def service(req, res)
  method_name = "do_" + req.request_method.gsub(/-/, "_")
  if respond_to?(method_name)
    __send__(method_name, req, res)
    raise HTTPStatus::MethodNotAllowed,
          "unsupported method `#{req.request_method}'."

Private Instance Methods

redirect_to_directory_uri(req, res) click to toggle source

Redirects to a path ending in /

# File lib/webrick/httpservlet/abstract.rb, line 141
def redirect_to_directory_uri(req, res)
  if req.path[-1] != ?/
    location = WEBrick::HTTPUtils.escape_path(req.path + "/")
    if req.query_string && req.query_string.bytesize > 0
      location << "?" << req.query_string
    res.set_redirect(HTTPStatus::MovedPermanently, location)