Sha256: ec553ca87c2694fe0934f1b50ea5373a916af72e5dfb9b4c12a4fb0e69a8af1d

Contents?: true

Size: 965 Bytes

Versions: 1

Compression:

Stored size: 965 Bytes

Contents

module Rack

  module SlashEnforce
    def self.version
      '0.0.2'
    end
  end

  class RemoveTrailingSlashes
    # regexp to match strings that start and end with a slash
    MATCH = %r{^/(.*)/$}

    def initialize(app)
      @app = app
    end

    def call(env)
      if env['PATH_INFO'] != '/' && env['PATH_INFO'] =~ MATCH
        env['PATH_INFO'].sub!(/(\/)+$/,'')
        [301, {"Location" => Rack::Request.new(env).url, "Content-Type" => ""}, []]
      else
        @app.call(env)
      end
    end
  end

  class AppendTrailingSlash
    # regexp to match strings without periods that start and end with a slash
    MATCH = %r{^/([^.]*)[^/]$}

    def initialize(app)
      @app = app
    end

    def call(env)
      if env['PATH_INFO'] != '/' && env['PATH_INFO'] =~ MATCH
        env['PATH_INFO'] += '/'
        [301, {"Location" => Rack::Request.new(env).url, "Content-Type" => ""}, []]
      else
        @app.call(env)
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-slashenforce-0.0.2 lib/rack-slashenforce.rb