summaryrefslogtreecommitdiff
path: root/lib/webrick/httpserver.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/webrick/httpserver.rb')
-rw-r--r--lib/webrick/httpserver.rb60
1 files changed, 45 insertions, 15 deletions
diff --git a/lib/webrick/httpserver.rb b/lib/webrick/httpserver.rb
index ddf1ac7404..e85d059319 100644
--- a/lib/webrick/httpserver.rb
+++ b/lib/webrick/httpserver.rb
@@ -1,3 +1,4 @@
+# frozen_string_literal: false
#
# httpserver.rb -- HTTPServer Class
#
@@ -8,13 +9,14 @@
#
# $IPR: httpserver.rb,v 1.63 2002/10/01 17:16:32 gotoyuzo Exp $
-require 'webrick/server'
-require 'webrick/httputils'
-require 'webrick/httpstatus'
-require 'webrick/httprequest'
-require 'webrick/httpresponse'
-require 'webrick/httpservlet'
-require 'webrick/accesslog'
+require 'io/wait'
+require_relative 'server'
+require_relative 'httputils'
+require_relative 'httpstatus'
+require_relative 'httprequest'
+require_relative 'httpresponse'
+require_relative 'httpservlet'
+require_relative 'accesslog'
module WEBrick
class HTTPServerError < ServerError; end
@@ -66,17 +68,17 @@ module WEBrick
def run(sock)
while true
- res = HTTPResponse.new(@config)
- req = HTTPRequest.new(@config)
+ req = create_request(@config)
+ res = create_response(@config)
server = self
begin
timeout = @config[:RequestTimeout]
while timeout > 0
- break if IO.select([sock], nil, nil, 0.5)
- timeout = 0 if @status != :Running
+ break if sock.to_io.wait_readable(0.5)
+ break if @status != :Running
timeout -= 0.5
end
- raise HTTPStatus::EOFError if timeout <= 0
+ raise HTTPStatus::EOFError if timeout <= 0 || @status != :Running
raise HTTPStatus::EOFError if sock.eof?
req.parse(sock)
res.request_method = req.request_method
@@ -138,6 +140,10 @@ module WEBrick
si.service(req, res)
end
+ ##
+ # The default OPTIONS request handler says GET, HEAD, POST and OPTIONS
+ # requests are allowed.
+
def do_OPTIONS(req, res)
res["allow"] = "GET,HEAD,POST,OPTIONS"
end
@@ -207,6 +213,10 @@ module WEBrick
}
end
+ ##
+ # Logs +req+ and +res+ in the access logs. +config+ is used for the
+ # server name.
+
def access_log(config, req, res)
param = AccessLog::setup_params(config, req, res)
@config[:AccessLog].each{|logger, fmt|
@@ -214,7 +224,27 @@ module WEBrick
}
end
- class MountTable
+ ##
+ # Creates the HTTPRequest used when handling the HTTP
+ # request. Can be overridden by subclasses.
+ def create_request(with_webrick_config)
+ HTTPRequest.new(with_webrick_config)
+ end
+
+ ##
+ # Creates the HTTPResponse used when handling the HTTP
+ # request. Can be overridden by subclasses.
+ def create_response(with_webrick_config)
+ HTTPResponse.new(with_webrick_config)
+ end
+
+ ##
+ # Mount table for the path a servlet is mounted on in the directory space
+ # of the server. Users of WEBrick can only access this indirectly via
+ # WEBrick::HTTPServer#mount, WEBrick::HTTPServer#unmount and
+ # WEBrick::HTTPServer#search_servlet
+
+ class MountTable # :nodoc:
def initialize
@tab = Hash.new
compile
@@ -251,12 +281,12 @@ module WEBrick
k.sort!
k.reverse!
k.collect!{|path| Regexp.escape(path) }
- @scanner = Regexp.new("^(" + k.join("|") +")(?=/|$)")
+ @scanner = Regexp.new("\\A(" + k.join("|") +")(?=/|\\z)")
end
def normalize(dir)
ret = dir ? dir.dup : ""
- ret.sub!(%r|/+$|, "")
+ ret.sub!(%r|/+\z|, "")
ret
end
end