3

如何在 WEBrick 中处理 PUT 请求?

我尝试在 AbstractServlet 类中定义一个 do_PUT() 方法,但该方法从未被调用。

4

1 回答 1

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

然后你需要使用你自己的自定义类来启动你的代理服务器。

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)
于 2013-09-25T18:03:37.480 回答