Class: Rack::UrlCanonicalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/url_canonicalizer.rb,
lib/rack/url_canonicalizer/railtie.rb,
lib/rack/url_canonicalizer/version.rb,
lib/rack/url_canonicalizer/configuration.rb

Defined Under Namespace

Classes: Configuration, Railtie

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ UrlCanonicalizer

Returns a new instance of UrlCanonicalizer.



24
25
26
27
28
29
30
31
32
# File 'lib/rack/url_canonicalizer.rb', line 24

def initialize(app, options = {})
  @app = app
  @config = self.class.configuration.dup

  options.each do |key, value|
    writer = "#{key}="
    @config.send(writer, value) if @config.respond_to?(writer)
  end
end

Class Method Details

.configurationObject



11
12
13
# File 'lib/rack/url_canonicalizer.rb', line 11

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



15
16
17
# File 'lib/rack/url_canonicalizer.rb', line 15

def configure
  yield(configuration)
end

.reset_configuration!Object



19
20
21
# File 'lib/rack/url_canonicalizer.rb', line 19

def reset_configuration!
  @configuration = Configuration.new
end

Instance Method Details

#call(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rack/url_canonicalizer.rb', line 34

def call(env)
  req = Rack::Request.new(env)

  return @app.call(env) unless req.get? || req.head?
  return @app.call(env) if xhr_request?(req, env)

  path_info = env["PATH_INFO"] || ""

  if excluded_path?(path_info)
    return @app.call(env)
  end

  host = req.host || ""
  host_redirect = @config.strip_www && host.start_with?("www.")
  target_host = host_redirect ? host.sub(/\Awww\./, "") : host

  raw_path = path_info
  normalized_path = raw_path.dup

  if @config.collapse_slashes
    normalized_path.gsub!(%r{/{2,}}, "/")
  end

  if @config.strip_trailing_slash && normalized_path.length > 1 && normalized_path.end_with?("/")
    normalized_path.chomp!("/")
  end

  query_params = req.GET.dup
  locale_redirect = false
  locale_key = @config.locale_param

  if locale_key && query_params.key?(locale_key)
    allowed = @config.allowed_locales_list
    if allowed && !allowed.include?(query_params[locale_key].to_s)
      query_params.delete(locale_key)
      locale_redirect = true
    end
  end

  if host_redirect || raw_path != normalized_path || locale_redirect
    scheme = req.scheme
    port = req.port
    port_part = [ 80, 443 ].include?(port) ? "" : ":#{port}"

    new_query = Rack::Utils.build_nested_query(query_params)
    new_url = +"#{scheme}://#{target_host}#{port_part}#{normalized_path}"
    new_url << "?#{new_query}" unless new_query.empty?

    return [
      @config.redirect_status,
      {
        "location" => new_url,
        "content-type" => "text/html",
        "cache-control" => @config.cache_control
      },
      [ redirect_body(@config.redirect_status) ]
    ]
  end

  @app.call(env)
end