在WEBrick中处理PUT方法

时间:2011-02-14 19:01:59

标签: ruby webrick

如何处理WEBrick中的PUT请求?

我已尝试在AbstractServlet类中定义do_PUT()方法,但从不调用该方法。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,并通过创建我自己的自定义WEBrick :: HTTPProxyServer并在其中添加put方法来实现它。

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

然后,您需要使用自己的Custom类启动代理服务器。

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)