/tools/Ruby/lib/ruby/1.8/webrick/httpservlet/abstract.rb

http://github.com/agross/netopenspace · Ruby · 71 lines · 50 code · 12 blank · 9 comment · 5 complexity · 2d0565f837d587127de9e97d965b5c14 MD5 · raw file

  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. require 'thread'
  11. require 'webrick/htmlutils'
  12. require 'webrick/httputils'
  13. require 'webrick/httpstatus'
  14. module WEBrick
  15. module HTTPServlet
  16. class HTTPServletError < StandardError; end
  17. class AbstractServlet
  18. def self.get_instance(config, *options)
  19. self.new(config, *options)
  20. end
  21. def initialize(server, *options)
  22. @server = @config = server
  23. @logger = @server[:Logger]
  24. @options = options
  25. end
  26. def service(req, res)
  27. method_name = "do_" + req.request_method.gsub(/-/, "_")
  28. if respond_to?(method_name)
  29. __send__(method_name, req, res)
  30. else
  31. raise HTTPStatus::MethodNotAllowed,
  32. "unsupported method `#{req.request_method}'."
  33. end
  34. end
  35. def do_GET(req, res)
  36. raise HTTPStatus::NotFound, "not found."
  37. end
  38. def do_HEAD(req, res)
  39. do_GET(req, res)
  40. end
  41. def do_OPTIONS(req, res)
  42. m = self.methods.grep(/^do_[A-Z]+$/)
  43. m.collect!{|i| i.sub(/do_/, "") }
  44. m.sort!
  45. res["allow"] = m.join(",")
  46. end
  47. private
  48. def redirect_to_directory_uri(req, res)
  49. if req.path[-1] != ?/
  50. location = WEBrick::HTTPUtils.escape_path(req.path + "/")
  51. if req.query_string && req.query_string.size > 0
  52. location << "?" << req.query_string
  53. end
  54. res.set_redirect(HTTPStatus::MovedPermanently, location)
  55. end
  56. end
  57. end
  58. end
  59. end