11# frozen_string_literal: true
22
3- require " rack"
4- require_relative " url_canonicalizer/version"
5- require_relative " url_canonicalizer/configuration"
6- require_relative " url_canonicalizer/railtie"
3+ require ' rack'
4+ require_relative ' url_canonicalizer/version'
5+ require_relative ' url_canonicalizer/configuration'
6+ require_relative ' url_canonicalizer/railtie'
77
88module Rack
99 class UrlCanonicalizer
@@ -37,25 +37,21 @@ def call(env)
3737 return @app . call ( env ) unless req . get? || req . head?
3838 return @app . call ( env ) if xhr_request? ( req , env )
3939
40- path_info = env [ " PATH_INFO" ] || ""
40+ path_info = env [ ' PATH_INFO' ] || ''
4141
42- if excluded_path? ( path_info )
43- return @app . call ( env )
44- end
42+ return @app . call ( env ) if excluded_path? ( path_info )
4543
46- host = req . host || ""
47- host_redirect = @config . strip_www && host . start_with? ( " www." )
48- target_host = host_redirect ? host . sub ( /\A www\. / , "" ) : host
44+ host = req . host || ''
45+ host_redirect = @config . strip_www && host . start_with? ( ' www.' )
46+ target_host = host_redirect ? host . sub ( /\A www\. / , '' ) : host
4947
5048 raw_path = path_info
5149 normalized_path = raw_path . dup
5250
53- if @config . collapse_slashes
54- normalized_path . gsub! ( %r{/{2,}} , "/" )
55- end
51+ normalized_path . gsub! ( %r{/{2,}} , '/' ) if @config . collapse_slashes
5652
57- if @config . strip_trailing_slash && normalized_path . length > 1 && normalized_path . end_with? ( "/" )
58- normalized_path . chomp! ( "/" )
53+ if @config . strip_trailing_slash && normalized_path . length > 1 && normalized_path . end_with? ( '/' )
54+ normalized_path . chomp! ( '/' )
5955 end
6056
6157 query_params = req . GET . dup
@@ -73,20 +69,20 @@ def call(env)
7369 if host_redirect || raw_path != normalized_path || locale_redirect
7470 scheme = req . scheme
7571 port = req . port
76- port_part = [ 80 , 443 ] . include? ( port ) ? "" : ":#{ port } "
72+ port_part = [ 80 , 443 ] . include? ( port ) ? '' : ":#{ port } "
7773
7874 new_query = Rack ::Utils . build_nested_query ( query_params )
79- new_url = + "#{ scheme } ://#{ target_host } #{ port_part } #{ normalized_path } "
75+ new_url = "#{ scheme } ://#{ target_host } #{ port_part } #{ normalized_path } "
8076 new_url << "?#{ new_query } " unless new_query . empty?
8177
8278 return [
8379 @config . redirect_status ,
8480 {
85- " location" => new_url ,
86- " content-type" => " text/html" ,
87- " cache-control" => @config . cache_control
81+ ' location' => new_url ,
82+ ' content-type' => ' text/html' ,
83+ ' cache-control' => @config . cache_control
8884 } ,
89- [ redirect_body ( @config . redirect_status ) ]
85+ [ redirect_body ( @config . redirect_status ) ]
9086 ]
9187 end
9288
@@ -98,7 +94,7 @@ def call(env)
9894 def xhr_request? ( req , env )
9995 return true if req . respond_to? ( :xhr? ) && req . xhr?
10096
101- env [ " HTTP_X_REQUESTED_WITH" ] == " XMLHttpRequest"
97+ env [ ' HTTP_X_REQUESTED_WITH' ] == ' XMLHttpRequest'
10298 end
10399
104100 def excluded_path? ( path_info )
@@ -111,11 +107,11 @@ def excluded_path?(path_info)
111107
112108 def redirect_body ( status )
113109 case status
114- when 301 then " Moved Permanently"
115- when 302 then " Found"
116- when 307 then " Temporary Redirect"
117- when 308 then " Permanent Redirect"
118- else " Redirected"
110+ when 301 then ' Moved Permanently'
111+ when 302 then ' Found'
112+ when 307 then ' Temporary Redirect'
113+ when 308 then ' Permanent Redirect'
114+ else ' Redirected'
119115 end
120116 end
121117 end
0 commit comments