Skip to content

Instantly share code, notes, and snippets.

@slowernet
Last active February 17, 2016 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slowernet/b43c9014800f639842f4 to your computer and use it in GitHub Desktop.
Save slowernet/b43c9014800f639842f4 to your computer and use it in GitHub Desktop.
# prepend the mime type suggested by the path_info extname to the Accept: header,
# and pass on the request with suffix removed
module Rack
class SuffixAccept
def initialize(app)
@app = app
end
def call(env)
req = Rack::Request.new(env)
extname = ::File.extname(req.path_info)
if !extname.empty? && !(mime_type = Rack::Mime.mime_type(extname)).empty?
env['HTTP_ACCEPT'] = mime_type + ',' + env['HTTP_ACCEPT']
env['PATH_INFO'] = ::File.join(::File.dirname(req.path_info), ::File.basename(req.path_info, extname))
end
@app.call(env)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment