Handle the PUT method in WEBrick

578 Views Asked by At

How do I handle PUT requests in WEBrick?

I have tried defining a do_PUT() method in an AbstractServlet class but the method is never invoked.

1

There are 1 best solutions below

0
On

I had the same problem and got it working by creating my own custom WEBrick::HTTPProxyServer and adding the put method in that.

require "webrick"
require "webrick/httpproxy"
require 'cgi'

class CustomWEBrickProxyServer < WEBrick::HTTPProxyServer

  def do_PUT(req, res)
    perform_proxy_request(req, res) do |http, path, header|
      http.put(path, req.body || "", header)
    end
  end

  # This method is not needed for PUT but I added for completeness
  def do_OPTIONS(req, res)
    res['allow'] = "GET,HEAD,POST,OPTIONS,CONNECT,PUT"
  end

end

Then you need to start your proxy server using your own Custom class.

my_proxy_server = CustomWEBrickProxyServer.new :Port=> proxy_port,
                                               :ProxyVia => forward_proxy,
                                               :ProxyURI => forward_proxy,
                                               :RequestCallback => method(:request_callback),
                                               :ProxyContentHandler => method(:response_callback),
                                               :AccessLog => method(:access_log)