Sha256: a5fac994e2c88d4562712b5f6eb3977785f37bc0c0cd7843baed388578efe7a3

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

module Rack

  module SlashEnforce
    def self.version
      '0.0.3'
    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['REQUEST_METHOD'] == 'GET'
        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['REQUEST_METHOD'] == 'GET'
        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.3 lib/rack-slashenforce.rb