Sha256: 228eb17e5206bed7583de9174fd6ca3df437ac2ba0124de4a3c68d37171f4d65

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module Koi
  class UrlRedirect
    def initialize(app)
      @app = app
    end

    def call(env)
      status, headers, body = @app.call(env)

      current_path = env["REQUEST_PATH"]

      if status.to_i == 404 && current_path.exclude?("mini-profiler-resources") &&
          (new_path = find_redirect(current_path))
        request = ActionDispatch::Request.new(env)

        # Close the body as we will not use it for a 301 redirect
        body.close

        # Issue a "Moved permanently" response with the redirect location
        [301, { "Location" => new_location(current_path, new_path, request) },
         ["#{current_path} moved. The document has moved to #{new_path}"]]
      else
        # Not a 404 or no redirect found, just send the response as is
        [status, headers, body]
      end
    end

    private

    def new_location(current_path, new_path, request)
      if %r{\Ahttps{0,1}://}.match?(new_path)
        new_path
      else
        request.original_url.gsub(current_path, new_path)
      end
    end

    def find_redirect(path)
      UrlRewrite.redirect_path_for(path)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
katalyst-koi-4.1.0 lib/koi/middleware/url_redirect.rb
katalyst-koi-4.0.3 lib/koi/middleware/url_redirect.rb
katalyst-koi-4.0.2 lib/koi/middleware/url_redirect.rb
katalyst-koi-4.0.1 lib/koi/middleware/url_redirect.rb
katalyst-koi-4.0.0 lib/koi/middleware/url_redirect.rb