From 5e2e2c5fead34636a34ff4d722981a309cf8e279 Mon Sep 17 00:00:00 2001 From: Julik Tarkhanov Date: Tue, 16 Jun 2015 12:49:21 +0200 Subject: [PATCH] Add the ability to override res, req creation so that a customized HTTPServer subclass can use it's own Request/Response classes. To apply the override, make a subclass of WEBrick::HTTPServer and override the `create_request_and_response(with_webrick_config)` method. The method should return an Array of [request, response]. To check whether the Server supports this method (i.e. when using older versions of WEBrick when needing this functionality), you can ask the server if it responds to the method server.respond_to?(:create_request_and_response) This is backportable. --- lib/webrick/httpserver.rb | 12 +++++++++--- test/webrick/test_httpserver.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/webrick/httpserver.rb b/lib/webrick/httpserver.rb index 96bd3fa..b8d07c9 100644 --- a/lib/webrick/httpserver.rb +++ b/lib/webrick/httpserver.rb @@ -67,8 +67,7 @@ module WEBrick def run(sock) while true - res = HTTPResponse.new(@config) - req = HTTPRequest.new(@config) + req, res = create_request_and_response(@config) server = self begin timeout = @config[:RequestTimeout] @@ -222,7 +221,14 @@ module WEBrick logger << AccessLog::format(fmt+"\n", param) } end - + + ## + # Creates the HTTPRequest and HTTPResponse objects used when handling the HTTP + # request. Can be overridden by subclasses. + def create_request_and_response(with_webrick_config) + [HTTPRequest.new(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 diff --git a/test/webrick/test_httpserver.rb b/test/webrick/test_httpserver.rb index 5cd4ba5..f057f64 100644 --- a/test/webrick/test_httpserver.rb +++ b/test/webrick/test_httpserver.rb @@ -269,6 +269,34 @@ 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_and_response(config) + [CustomRequest.new(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 -- 2.2.1