Rack middleware: Rewriting content types

Language: Ruby

# Rewrite content types based on file extensions
class RewriteContentType
  def initialize app, opts
    @app = app
    @map = opts
  end

  def call env
    res = @app.call(env)
    ext = env["PATH_INFO"].split(".")[-1]
    res[1]["Content-Type"] = @map[ext] if @map.has_key?(ext)
    res
  end
end

# Inside config.ru
use RewriteContentType, {"js" => "text/javascript"}
Reveal More
Added over 2 years ago by Twitterprofilephoto_normal zh