summary refs log tree commit
diff options
context:
space:
mode:
authorJames Tucker <jftucker@gmail.com>2013-07-04 16:17:43 -0700
committerJames Tucker <jftucker@gmail.com>2013-07-04 16:17:43 -0700
commit977351595d7ff7d17cc4c4b2526922ef37b7c65e (patch)
tree841e380026d63c45622a8ee3b4e6379b2e7d88da
parent371a65a2a2cd470a86d45eabdb3de5b5d6a63173 (diff)
downloadrack-977351595d7ff7d17cc4c4b2526922ef37b7c65e.tar.gz
Refactor rack static header rules application
-rw-r--r--lib/rack/static.rb49
1 files changed, 23 insertions, 26 deletions
diff --git a/lib/rack/static.rb b/lib/rack/static.rb
index 6b0762a3..41aec7f3 100644
--- a/lib/rack/static.rb
+++ b/lib/rack/static.rb
@@ -113,7 +113,12 @@ module Rack
         env["PATH_INFO"] = (path =~ /\/$/ ? path + @index : @urls[path]) if overwrite_file_path(path)
         path = env["PATH_INFO"]
         response = @file_server.call(env)
-        apply_header_rules(response[1], path)
+
+        headers = response[1]
+        applicable_rules(path).each do |rule, new_headers|
+          new_headers.each { |field, content| headers[field] = content }
+        end
+
         response
       else
         @app.call(env)
@@ -121,33 +126,25 @@ module Rack
     end
 
     # Convert HTTP header rules to HTTP headers
-    def apply_header_rules(headers, path)
-      @header_rules.each do |rule, new_headers|
-        apply_rule(headers, path, rule, new_headers)
-      end
-    end
-
-    def apply_rule(headers, path, rule, new_headers)
-      case rule
-      when :all    # All files
-        set_headers(headers, new_headers)
-      when :fonts  # Fonts Shortcut
-        set_headers(headers, new_headers) if path.match(/\.(?:ttf|otf|eot|woff|svg)\z/)
-      when String  # Folder
-        path = ::Rack::Utils.unescape(path)
-        set_headers(headers, new_headers) if (path.start_with?(rule) || path.start_with?('/' + rule))
-      when Array   # Extension/Extensions
-        extensions = rule.join('|')
-        set_headers(headers, new_headers) if path.match(/\.(#{extensions})\z/)
-      when Regexp  # Flexible Regexp
-        set_headers(headers, new_headers) if path.match(rule)
-      else
+    def applicable_rules(path)
+      @header_rules.find_all do |rule, new_headers|
+        case rule
+        when :all
+          true
+        when :fonts
+          path =~ /\.(?:ttf|otf|eot|woff|svg)\z/
+        when String
+          path = ::Rack::Utils.unescape(path)
+          path.start_with?(rule) || path.start_with?('/' + rule)
+        when Array
+          path =~ /\.(#{rule.join('|')})\z/
+        when Regexp
+          path =~ rule
+        else
+          false
+        end
       end
     end
 
-    def set_headers(headers, new_headers)
-      new_headers.each { |field, content| headers[field] = content }
-    end
-
   end
 end