class Da99_Rack_Protect::No_Slash_Path_Ending

Constants

LAST_SLASH
METHODS
SLASH

Public Class Methods

new(new_app) click to toggle source
# File lib/da99_rack_protect/0030_No_Slash_Path_Ending.rb, line 8
def initialize new_app
  @app = new_app
end

Public Instance Methods

call(new_env) click to toggle source
# File lib/da99_rack_protect/0030_No_Slash_Path_Ending.rb, line 12
def call new_env

  remove_slash = begin
                   new_env['PATH_INFO'] != SLASH &&
                     METHODS.include?(new_env['REQUEST_METHOD']) &&
                     new_env['PATH_INFO'][-1,1] == SLASH &&
                     File.extname(new_env['PATH_INFO']) === ''
                 end

  return(@app.call( new_env )) unless remove_slash

  req  = Rack::Request.new(new_env)
  response = Rack::Response.new

  qs = req.query_string.strip.empty? ? nil : req.query_string
  new = [ req.path_info.sub(LAST_SLASH, ''), qs ].compact.join('?')

  response.redirect( new, 301 ) # permanent
  response.finish

end