Last active
February 17, 2016 02:17
-
-
Save slowernet/b43c9014800f639842f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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