Search code examples
rubywebrick

Adding PUT/DELETE to Ruby WEBrick


I'm trying to add PUT and DELETE verbs to WEBrick. I don't need them to do anything. They just need to respond with a 200. Below is the script I am running. GET works, but DELETE returns 405 with a "unsupported method DELETE" message. Can anyone tell me what is wrong or missing with this code?

require 'webrick'

module WEBrick
  module HTTPServlet
    class ProcHandler
      alias do_PUT    do_GET
      alias do_DELETE do_GET
    end
  end
end

sRoot = "C:\\"

server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => sRoot

trap "INT" do server.shutdown end

server.start

Solution

  • I figured it out. I had to add appropriate handlers to DefaultFileHandler. @kimmo, thanks for the tip!

    require 'webrick'
    
    module WEBrick
      module HTTPServlet
        class FileHandler
          alias do_PUT    do_GET
          alias do_DELETE do_GET
        end
    
        class DefaultFileHandler
          def do_DELETE(req, res)
            res.body = ''
          end
    
          def do_PUT(req, res)
            res.body = ''
          end
        end
      end
    end
    
    sRoot = "C:\\"
    
    server = WEBrick::HTTPServer.new :Port => 8000, :DocumentRoot => sRoot
    
    trap "INT" do server.shutdown end
    
    server.start