[前][次][番号順一覧][スレッド一覧]

ruby-changes:54231

From: normal <ko1@a...>
Date: Wed, 19 Dec 2018 20:08:13 +0900 (JST)
Subject: [ruby-changes:54231] normal:r66452 (trunk): webrick: add the ability to override res, req creation

normal	2018-12-19 20:08:05 +0900 (Wed, 19 Dec 2018)

  New Revision: 66452

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=66452

  Log:
    webrick: 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.
    
    [ruby-core:69604] [Feature #11266]
    
    From: Julik Tarkhanov <me@j...>

  Modified files:
    trunk/lib/webrick/httpserver.rb
    trunk/test/webrick/test_httpserver.rb
Index: test/webrick/test_httpserver.rb
===================================================================
--- test/webrick/test_httpserver.rb	(revision 66451)
+++ test/webrick/test_httpserver.rb	(revision 66452)
@@ -276,6 +276,38 @@ class TestWEBrickHTTPServer < Test::Unit https://github.com/ruby/ruby/blob/trunk/test/webrick/test_httpserver.rb#L276
     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
Index: lib/webrick/httpserver.rb
===================================================================
--- lib/webrick/httpserver.rb	(revision 66451)
+++ lib/webrick/httpserver.rb	(revision 66452)
@@ -68,8 +68,8 @@ module WEBrick https://github.com/ruby/ruby/blob/trunk/lib/webrick/httpserver.rb#L68
 
     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 https://github.com/ruby/ruby/blob/trunk/lib/webrick/httpserver.rb#L225
     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

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]