开发者

Handle the PUT method in WEBrick

开发者 https://www.devze.com 2023-02-11 13:05 出处:网络
How do I handle PUT requests in WEBrick? I have tried defining a do_PUT() method in an Abst开发者_StackOverflow社区ractServlet class but the method is never invoked.I had the same problem and got it

How do I handle PUT requests in WEBrick?

I have tried defining a do_PUT() method in an Abst开发者_StackOverflow社区ractServlet class but the method is never invoked.


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消