summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/webrick/httpserver.rb18
-rw-r--r--test/webrick/test_httpserver.rb32
2 files changed, 48 insertions, 2 deletions
diff --git a/lib/webrick/httpserver.rb b/lib/webrick/httpserver.rb
index 6945868ec8..e85d059319 100644
--- a/lib/webrick/httpserver.rb
+++ b/lib/webrick/httpserver.rb
@@ -68,8 +68,8 @@ 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]
@@ -225,6 +225,20 @@ module WEBrick
end
##
+ # 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
diff --git a/test/webrick/test_httpserver.rb b/test/webrick/test_httpserver.rb
index 77da797282..a6e70da7e8 100644
--- a/test/webrick/test_httpserver.rb
+++ b/test/webrick/test_httpserver.rb
@@ -276,6 +276,38 @@ class TestWEBrickHTTPServer < Test::Unit::TestCase
assert_equal(stopped, 1)
end
+ class CustomRequest < ::WEBrick::HTTPRequest; end
+ class CustomResponse < ::WEBrick::HTTPResponse; end
+ class CustomServer < ::WEBrick::HTTPServer
+ def create_request(config)
+ CustomRequest.new(config)
+ end
+
+ def create_response(config)
+ CustomResponse.new(config)
+ end
+ end
+
+ def test_custom_server_request_and_response
+ config = { :ServerName => "localhost" }
+ TestWEBrick.start_server(CustomServer, config){|server, addr, port, log|
+ server.mount_proc("/", lambda {|req, res|
+ assert_kind_of(CustomRequest, req)
+ assert_kind_of(CustomResponse, res)
+ res.body = "via custom response"
+ })
+ Thread.pass while server.status != :Running
+
+ Net::HTTP.start(addr, port) do |http|
+ req = Net::HTTP::Get.new("/")
+ http.request(req){|res|
+ assert_equal("via custom response", res.body)
+ }
+ server.shutdown
+ end
+ }
+ end
+
# This class is needed by test_response_io_with_chunked_set method
class EventManagerForChunkedResponseTest
def initialize