Issue #11266 has been updated by Julik Tarkhanov.


Nobu, is there something else I can do?

----------------------------------------
Feature #11266: [PATCH] WEBrick: allow subclassing of Response and Request
https://bugs.ruby-lang.org/issues/11266#change-53371

* Author: Julik Tarkhanov
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
To properly support the Rack specification features introduced in 2013, a number of features is needed in HTTPResponse.

* The Response should be able to handle it's socket to the given proc for writing (the socket control should be transferred to another routine). This is required for rack.hijack to work.
* The Response assumes an "IO pull" model via read()/readpartial() for grabbing data from the body. Rack assumes a "push" model for writing the response, so having the ability to take over the socket is better for Rack as well. We can make a Fiber-based adapter to let an iterable object present itself like an IO to WEBrick but having direct socket control is much easier.
* The Response should be able to take control of the chunking. This is currently done as a monkeypatch in Rack, but we much rather have it contained in the Response objects used by the Rack-supplied server handler.

Rack currently solves it by giving WEBrick the read end of an IO pipe, which has a side effect of buffering the entire response even when direct socket operation is needed. This creates a situation where no Rack-based streaming servers can function (no long-polling, no server-sent-events, no large streamed responses via HTTP/FTP adapters etc.)

Since all the three of those require patches to the HTTPResponse, a better approach would be to let the Server object create the right Response and let that response deal with the socket in a way it sees fit. This, however, is impossible at the moment because HTTPResponse and HTTPRequest are hardcoded in the main Server loop code, so if you want to override them with your implementations you have to override the entire service() method.

If we let the Server object instantiate the Response and Request in it's own separate method a Server subclass could use customized versions of those. Pretty much the only downside is one extra method call per HTTP request. Rack could then use it's own Server subclass with the right Response and Request objects in place.

I will be happy to provide a patch.


---Files--------------------------------
custom_req_response.patch (3.25 KB)
req_response_sub2.patch (2.54 KB)


-- 
https://bugs.ruby-lang.org/