1 #
2 # httpservlet.rb -- HTTPServlet Module
3 #
4 # Author: IPR -- Internet Programming with Ruby -- writers
5 # Copyright (c) 2000 TAKAHASHI Masayoshi, GOTOU Yuuzou
6 # Copyright (c) 2002 Internet Programming with Ruby writers. All rights
7 # reserved.
8 #
9 # $IPR: abstract.rb,v 1.24 2003/07/11 11:16:46 gotoyuzo Exp $
10
11 require 'thread'
12
13 require 'webrick/htmlutils'
14 require 'webrick/httputils'
15 require 'webrick/httpstatus'
16
17 module WEBrick
18 module HTTPServlet
19 class HTTPServletError < StandardError; end
20
21 class AbstractServlet
22 def self.get_instance(config, *options)
23 self.new(config, *options)
24 end
25
26 def initialize(server, *options)
27 @server = @config = server
28 @logger = @server[:Logger]
29 @options = options
30 end
31
32 def service(req, res)
33 method_name = "do_" + req.request_method.gsub(/-/, "_")
34 if respond_to?(method_name)
35 __send__(method_name, req, res)
36 else
37 raise HTTPStatus::MethodNotAllowed,
38 "unsupported method `#{req.request_method}'."
39 end
40 end
41
42 def do_GET(req, res)
43 raise HTTPStatus::NotFound, "not found."
44 end
45
46 def do_HEAD(req, res)
47 do_GET(req, res)
48 end
49
50 def do_OPTIONS(req, res)
51 m = self.methods.grep(/^do_[A-Z]+$/)
52 m.collect!{|i| i.sub(/do_/, "") }
53 m.sort!
54 res["allow"] = m.join(",")
55 end
56
57 private
58
59 def redirect_to_directory_uri(req, res)
60 if req.path[-1] != ?/
61 location = WEBrick::HTTPUtils.escape_path(req.path + "/")
62 if req.query_string && req.query_string.size > 0
63 location << "?" << req.query_string
64 end
65 res.set_redirect(HTTPStatus::MovedPermanently, location)
66 end
67 end
68 end
69
70 end
71 end