Class: WEBrick::HTTPServlet::AbstractServlet
- Inherits:
-
Object
- Object
- WEBrick::HTTPServlet::AbstractServlet
- Defined in:
- lib/webrick/httpservlet/abstract.rb
Overview
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
end
def do_stuff_with request
return 200, 'text/plain', 'you got a page'
end
end
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
end
def do_stuff_with request
content = "<p " \
%q{style="color: #{@color}; font-size: #{@size}"} \
">Hello, World!"
return 200, "text/html", content
end
end
This servlet must be provided two arguments at mount time:
server.mount '/configurable', Configurable, 'red', '2em'
Direct Known Subclasses
CGIHandler, DefaultFileHandler, ERBHandler, FileHandler, ProcHandler
Class Method Summary collapse
-
.get_instance(server, *options) ⇒ Object
Factory for servlet instances that will handle a request from
server
usingoptions
from the mount point.
Instance Method Summary collapse
-
#do_GET(req, res) ⇒ Object
Raises a NotFound exception.
-
#do_HEAD(req, res) ⇒ Object
Dispatches to do_GET.
-
#do_OPTIONS(req, res) ⇒ Object
Returns the allowed HTTP request methods.
-
#initialize(server, *options) ⇒ AbstractServlet
constructor
Initializes a new servlet for
server
usingoptions
which are stored as-is in @options. -
#service(req, res) ⇒ Object
Dispatches to a
do_
method based onreq
if such a method is available.
Constructor Details
#initialize(server, *options) ⇒ AbstractServlet
Initializes a new servlet for server
using options
which are stored as-is in @options. @logger is also provided.
91 92 93 94 95 |
# File 'lib/webrick/httpservlet/abstract.rb', line 91 def initialize(server, *) @server = @config = server @logger = @server[:Logger] @options = end |
Class Method Details
.get_instance(server, *options) ⇒ Object
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.
83 84 85 |
# File 'lib/webrick/httpservlet/abstract.rb', line 83 def self.get_instance(server, *) self.new(server, *) end |
Instance Method Details
#do_GET(req, res) ⇒ Object
Raises a NotFound exception
115 116 117 |
# File 'lib/webrick/httpservlet/abstract.rb', line 115 def do_GET(req, res) raise HTTPStatus::NotFound, "not found." end |
#do_HEAD(req, res) ⇒ Object
Dispatches to do_GET
122 123 124 |
# File 'lib/webrick/httpservlet/abstract.rb', line 122 def do_HEAD(req, res) do_GET(req, res) end |
#do_OPTIONS(req, res) ⇒ Object
Returns the allowed HTTP request methods
129 130 131 132 133 |
# File 'lib/webrick/httpservlet/abstract.rb', line 129 def do_OPTIONS(req, res) m = self.methods.grep(/\Ado_([A-Z]+)\z/) {$1} m.sort! res["allow"] = m.join(",") end |
#service(req, res) ⇒ Object
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.
102 103 104 105 106 107 108 109 110 |
# File 'lib/webrick/httpservlet/abstract.rb', line 102 def service(req, res) method_name = "do_" + req.request_method.gsub(/-/, "_") if respond_to?(method_name) __send__(method_name, req, res) else raise HTTPStatus::MethodNotAllowed, "unsupported method `#{req.request_method}'." end end |