1

Is it possible to modify the WEBrick HTTP response headers globally for a specific file extension, for example to serve all files with svgz extension to include the HTTP Header "Content-Encoding: gzip" in the HTTP response? I can't seem to figure out how to do this.

a2f0
  • 1,615
  • 2
  • 19
  • 28

1 Answers1

1

lib/dps/compression.rb

module Dps 
  class Compression
    def initialize(app)  
      @app = app  
    end  

    def call(env)  
      status, headers, response = @app.call(env)
      if File.extname(env['REQUEST_URI']) == ".svgz" && status == 200 
        headers["Content-Encoding"] = "gzip"
      else
        nil 
      end 
      [status, headers, response]
    end  
  end 
end

config/application.rb

config.autoload_paths += Dir["#{config.root}/lib/**/"]
config.middleware.insert_before("ActionDispatch::Static", "Dps::Compression")
a2f0
  • 1,615
  • 2
  • 19
  • 28
  • Ah @dps, now I understand better what you really were looking for... In that case, a custom middleware is probably a good idea, just take in care: a middleware is a new element to your entire (Rails) stack, so every request will run this code. I thought you just need and endpoint to fetch this kind of files... Your original question is perhaps little descriptive :) Would be nice if you edit a bit the original question and title... PD. I don't understand your last edition to this answer. – markets May 25 '15 at 22:30
  • Markets thank you for all of your help today. You are a good soul for spending so much time on this for me. I have removed my final edit (it turns out it was extraneous), and modified the original question for clarity. – a2f0 May 26 '15 at 01:49
  • you're welcome! that's why community matters :) Thanks for clarify your OP. – markets May 26 '15 at 08:32