Created
April 14, 2009 14:21
-
-
Save reinh/95203 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
module Rack | |
# | |
# A Rack middleware for automatically removing the format token at the end | |
# of a request path and adding its media type to the HTTP_ACCEPT header. | |
# | |
# MIT-License - Rein Henrichs | |
# | |
class FormatAccepts | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
req = Rack::Request.new(env) | |
if req.path_info =~ /(.*)(\..+)$/ | |
env["PATH_INFO"] = $1 | |
ext = $2 | |
accept = env['HTTP_ACCEPT'].split(',') rescue [] | |
media_type = Rack::Mime::MIME_TYPES[ext] | |
accept.unshift(media_type) if media_type | |
env['HTTP_ACCEPT'] = accept.join(',') | |
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