How to set header of reponses from WEBrick server when the request method is OPTIONS or when 404?

33 Views Asked by At

I am developing a tool that sets up a very simple WEBrick server for development purpose. I want to add additional headers to all responses from the server.

I just overrided WEBrick::HTTPServer#service as this answer says:

server = WEBrick::HTTPServer.new Port: port, DocumentRoot: path
def server.service request, response
    super
    response['Access-Control-Allow-Origin'] = '*'
    response['Cache-Control'] = 'no-cache'
end

This works fine except when the request method is OPTIONS or when the file does not exist (404). How to make it work in those cases?

0

There are 0 best solutions below